尝试扩展 ActionView::Helpers::FormBuilder

发布于 2024-08-25 23:14:30 字数 645 浏览 17 评论 0原文

我试图通过将一些逻辑移入 FormBuilder 来干燥一些代码。在阅读了有关如何选择和替代表单生成器的文档后,对我来说逻辑解决方案似乎是这样的。

在视图中

<% form_for @event, :builder => TestFormBuilder do |f| %>
    <%= f.test  %>
    <%= f.submit 'Update' %>
<% end %>

,然后在 app/helpers/application_helper.rb 中,

module ApplicationHelper
    class TestFormBuilder < ActionView::Helpers::FormBuilder
        def test
            puts 'apa'
        end
    end
end

但是,这在“form_for”处给了我一个错误,

  uninitialized constant ActionView::Base::CompiledTemplates::TestFormBuilder

我在哪里做错了?

I am trying to DRY up some code by moving some logic into the FormBuilder. After reading the documentation about how to select and alternative form builder the logical solution for me seemed to be something like this.

In the view

<% form_for @event, :builder => TestFormBuilder do |f| %>
    <%= f.test  %>
    <%= f.submit 'Update' %>
<% end %>

and then in app/helpers/application_helper.rb

module ApplicationHelper
    class TestFormBuilder < ActionView::Helpers::FormBuilder
        def test
            puts 'apa'
        end
    end
end

This, however, gives me an error at the "form_for"

  uninitialized constant ActionView::Base::CompiledTemplates::TestFormBuilder

Where am I doing it wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

会傲 2024-09-01 23:14:30

尝试使用:

form_for @event, :builder => ApplicationHelper::TestFormBuilder do |f|

try with :

form_for @event, :builder => ApplicationHelper::TestFormBuilder do |f|
遇到 2024-09-01 23:14:30

构建器类可以放置在模块文件中,内部或/和外部模块定义中,如下所示:

    # app/helpers/events_helper.rb
    module EventsHelper
        ...
        class FormBuilderIn < ActionView::Helpers::FormBuilder
            ...
        end
    end
    class FormBuilderOut < ActionView::Helpers::FormBuilder
        ...
    end

将构建器附加到表单的正确方法是:

    # app/views/events/_form_in.html.erb
    form_for @event, :builder => EventsHelper::FormBuilderIn do |f|

    # app/views/events/_form_out.html.erb
    form_for @event, :builder => FormBuilderOut do |f|

这是在表单上设置构建器选项的帮助器方法:

    # app/helpers/events_helper.rb
    module EventsHelper
      def form_in_for(data, *args, &proc)
          options = args.extract_options!
          form_for(data, *(args << options.merge(:builder => EventsHelper::FormBuilderIn)), &proc)
      end
      def form_out_for(data, *args, &proc)
          options = args.extract_options!
          form_for(data, *(args << options.merge(:builder => FormBuilderOut)), &proc)
      end
    end
    ...

现在,有一种可选方法将构建器附加到form:

    # app/views/events/_form_in.html.erb
    form_in_for @event do |f|

    # app/views/events/_form_out.html.erb
    form_out_for @event do |f|

最后,自定义构建器可以放置在单独的文件夹中,例如“app/builders”,但这需要手动将此路径添加到应用程序环境中。对于 Rails 2.3.x,设置:

    # config/environment.rb.
    config.load_paths += %W( #{RAILS_ROOT}/app/builders )

Builder class can be placed in module file, inside or/and outside module definition, like this:

    # app/helpers/events_helper.rb
    module EventsHelper
        ...
        class FormBuilderIn < ActionView::Helpers::FormBuilder
            ...
        end
    end
    class FormBuilderOut < ActionView::Helpers::FormBuilder
        ...
    end

Proper way to attach builder to form is:

    # app/views/events/_form_in.html.erb
    form_for @event, :builder => EventsHelper::FormBuilderIn do |f|

    # app/views/events/_form_out.html.erb
    form_for @event, :builder => FormBuilderOut do |f|

Here is helper method for setting builder option on form:

    # app/helpers/events_helper.rb
    module EventsHelper
      def form_in_for(data, *args, &proc)
          options = args.extract_options!
          form_for(data, *(args << options.merge(:builder => EventsHelper::FormBuilderIn)), &proc)
      end
      def form_out_for(data, *args, &proc)
          options = args.extract_options!
          form_for(data, *(args << options.merge(:builder => FormBuilderOut)), &proc)
      end
    end
    ...

Now, there is optional way to attach builder to form:

    # app/views/events/_form_in.html.erb
    form_in_for @event do |f|

    # app/views/events/_form_out.html.erb
    form_out_for @event do |f|

Finally, custom builders can be placed in separated folder, for example "app/builders", but this demands to manually add this path into application environment. For Rails 2.3.x, set:

    # config/environment.rb.
    config.load_paths += %W( #{RAILS_ROOT}/app/builders )
我喜欢麦丽素 2024-09-01 23:14:30

正如您在 http://guides.rubyonrails.org/configuring.html# 中看到的配置动作视图,您可以为整个应用程序设置默认的FormBuilder类。在你的情况下:

config.action_view.default_form_builder = "ApplicationHelper::TestFormBuilder"

As you can see in http://guides.rubyonrails.org/configuring.html#configuring-action-view, you can set a default FormBuilder-class for your whole application. In your case:

config.action_view.default_form_builder = "ApplicationHelper::TestFormBuilder"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文