Rails 3 Custom FormBuilder Parameters

会有一股神秘感。 提交于 2019-12-25 01:44:45

问题


I create a custom FormBuilder for my application, but I dont understand why I need to pass tow parameters as follow:

f.text_field :my_model, :my_field

Here is my builder:

class AceBuilder < ActionView::Helpers::FormBuilder
  %w[text_field password_field text_area].each do |method_name|
    define_method(method_name) do |object_name, method, *args|
      options = args.extract_options!

      @template.content_tag(:div, :class => "control-group #{@object.errors[method].length > 0 ? 'error' : ''}") do
        @template.concat(
            @template.content_tag(:label,
                                  options[:label] || method.to_s.humanize,
                                  :class => 'control-label',
                                  :for => "#{object_name}_#{method}"))
        @template.concat(
            @template.content_tag(:div,
                                  :class => "controls") do
              @template.concat(super(method))

              @template.concat(
                  @template.content_tag(:span,
                                        @object.errors[method].join,
                                        :for => "#{object_name}_#{method}",
                                        :class => "help-inline"
                  )
              )
            end
        )
      end
    end
  end

I know that

define_method(method_name) do |object_name, method, *args|

has two parameters, so my question is:

How can I write this FormBuilder, such a way that I can call it like this:

f.text_field :my_field

Thanks.


回答1:


That was easy, after I realize how: replace object_name by @object_name

class AceBuilder < ActionView::Helpers::FormBuilder
  %w[text_field password_field text_area].each do |method_name|
    define_method(method_name) do |method, *args|
      options = args.extract_options!

      @template.content_tag(:div, :class => "control-group #{@object.errors[method].length > 0 ? 'error' : ''}") do
        @template.concat(
            @template.content_tag(:label,
                                  options[:label] || method.to_s.humanize,
                                  :class => 'control-label',
                                  :for => "#{@object_name}_#{method}"))
        @template.concat(
            @template.content_tag(:div,
                                  :class => "controls") do
              @template.concat(super(method))

              @template.concat(
                  @template.content_tag(:span,
                                        @object.errors[method].join,
                                        :for => "#{@object_name}_#{method}",
                                        :class => "help-inline"
                  )
              )
            end
        )
      end
    end
  end


来源:https://stackoverflow.com/questions/16607386/rails-3-custom-formbuilder-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!