rails 4, change default form builder globally

谁说我不能喝 提交于 2020-06-27 07:47:08

问题


I have sub-classed the default form builder to add some additional methods. Sample code is below.

module ApplicationHelper
    class AppFormBuilder < ActionView::Helpers::FormBuilder
        def coordinates_field(method, options = {})
            options[:readonly] = 'true'
            ....
            @template.text_field(@object_name, method, objectify_options(options))
        end
    end
end

This works well, but to use it I have to change the view code for every form that uses the coordinates_field method, i.e.,

<%= form_for @object, :builder => ApplicationHelper::AppFormBuilder do |f| %>
...
<% end %>

It sounds like it is theoretically possible to change the default form builder globally (config.action_view.default_form_builder), but I can't seem to get this to work. This is what I have tried in /config/application.rb:

module Web
  class Application < Rails::Application
    ...
    config.action_view.default_form_builder = "ApplicationHelper::AppFormBuilder"
  end
end

Which results in error undefined method 'new' for "ApplicationHelper::AppFormBuilder":String when I hit a view that has a form.

If I instead try this

config.action_view.default_form_builder = ApplicationHelper::AppFormBuilder

I get the error *config.action_view.default_form_builder = ApplicationHelper::AppFormBuilder* when the application starts.

Can anyone provide guidance on how to get this to work?


回答1:


ApplicationHelper::AppFormBuilder is not required yet at the time application.rb loads. You can try to put this in a separate initializer file (create one in config\initializers):

module Web
  class Application
    ActionView::Base.default_form_builder = AppFormBuilder
  end
end



回答2:


As mentioned in the official docs, in Rails 5 the correct way is to specify it in a controller. To make it application wide, just set in your ApplicationController:

class ApplicationController < ActionController::Base
  default_form_builder AppFormBuilder
end

This makes much more sense. FormHelpers are part of the view layer and not the application config.




回答3:


I like Max's answer, but (rails n00b here so YMMV) I believe this is equivalent and cleaner, directly in config/application.rb:

config.after_initialize do
  ActionView::Base.default_form_builder = MyCustomHelper::MyCustomFormBuilder
end

obviously you replace the names MyCustomHelper and MyCustomFormBuilder.

took me about 48hrs to figure this out, thanks to all that well-structured rails documentation.




回答4:


You should be able to set this from an initializer as you would for other config options. Create a default_form_builder.rb file under config/initializers/. The syntax should simpler than in @Max's answer.

Rails.application.config.action_view.default_form_builder = AppFormBuilder

I suggest you do not include this in a helper. Add it as separate class inside the /lib directory. You may or may not need to prefix the with the module it's contained within.

Finally, you can set this globally from config/application.rb, but you would have to pass it as a string since the class may not be loaded when rails starts up.

config.action_view.default_form_builder = 'AppFormBuilder'



回答5:


Rails 5+ 👌

class ApplicationController < ActionController::Base
  default_form_builder AppFormBuilder
end

Rails 4 🏖

class ApplicationController < ActionController::Base
  ActionView::Base.default_form_builder = AppFormBuilder
end

... or if you prefer an initializer, I'd recommend setting it as a string instead, that way you will not have to restart Rails every time you change something in your form builder.. 😴 (since the class may not be loaded when rails starts up. https://stackoverflow.com/a/27992240/2037928)

module Web
  class Application
    ActionView::Base.default_form_builder = "AppFormBuilder"
  end
end


来源:https://stackoverflow.com/questions/22711801/rails-4-change-default-form-builder-globally

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