Rails 中的自定义表单元素
所以我是 Rails 新手,我试图找出添加自定义表单元素的规范方法是什么。目前我的做法非常糟糕。
module ActionView
module Helpers
module FormOptionsHelper
def some_new_field(object, method, options = {}, html_options = {})
#code code
end
end
class FormBuilder
def contract_year_select(method, options = {}, html_options = {})
@template.some_new_field(@object_name, method, objectify_options(options), @default_options.merge(html_options))
end
end
end
end
然而我已经看到了这一点。
class Forms::ApplicationFormBuilder < ActionView::Helpers::FormBuilder
Forms::ApplicationHelper.instance_methods.each do |selector|
src = <<-end_src
def #{selector}(method, options = {})
@template.send(#{selector.inspect}, @object_name, method, objectify_options(options))
end
end_src
class_eval src, __FILE__, __LINE__
end
private
def objectify_options(options)
@default_options.merge(options.merge(:object => @object))
end
end
(来自此处)
扩展 FormBuilder 似乎是比鸭子打孔更好的解决方案。是否有其他方法可以做到这一点,而不需要直接将 FormBuilder 类的部分复制到我的自定义类中?
So I'm new to Rails and I'm trying to figure out what the canonical way to add custom form elements is. Currently the way I'm doing it is super grotty.
module ActionView
module Helpers
module FormOptionsHelper
def some_new_field(object, method, options = {}, html_options = {})
#code code
end
end
class FormBuilder
def contract_year_select(method, options = {}, html_options = {})
@template.some_new_field(@object_name, method, objectify_options(options), @default_options.merge(html_options))
end
end
end
end
I have seen this however.
class Forms::ApplicationFormBuilder < ActionView::Helpers::FormBuilder
Forms::ApplicationHelper.instance_methods.each do |selector|
src = <<-end_src
def #{selector}(method, options = {})
@template.send(#{selector.inspect}, @object_name, method, objectify_options(options))
end
end_src
class_eval src, __FILE__, __LINE__
end
private
def objectify_options(options)
@default_options.merge(options.merge(:object => @object))
end
end
(from here)
Extending FormBuilder seems like a better solution than duck punching it. Is there some other way to do this that doesn't require directly copying parts of the FormBuilder class into my custom one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有两个伙伴:
首先,作为 Rails 的新手,我建议使用 Formtastic gem。
V 是 Rails 中 MVC 中被滥用和忽视的部分。
Formtastic 经过充分测试,支持 Rails 3
https://github.com/justinfrench/formtastic
然后,您可以创建自定义元素来扩展 Formtastic 的出色类别,并获得除了简单的字段显示之外的一系列其他好处。
其次,延长班级是绝对可以接受的。我更喜欢 Module 方法,因为它更容易查看。就像这样的内容:
但我几乎不知道我在这里在说什么……这个东西就在我理解的边缘。
刚刚看到你谈论表单,我喜欢 Formtastic!
Two parter here:
First, as someone new to rails, I would recommend using the Formtastic gem.
V is the abused and neglected part of MVC in rails.
Formtastic is very well tested and has support for Rails 3
https://github.com/justinfrench/formtastic
With Formtastic you can then create custom elements extending it's brilliant class and get a whole host of other benefits beyond simple field display.
Second, extending the class is definitely acceptable. I prefer the Module method as it's easier to look at. Like something along the lines of:
But I barely know what I am talking about here... This stuff is right on the edge of my understanding.
Just saw you talking about forms and I love Formtastic!