本文介绍了尝试扩展 ActionView::Helpers::FormBuilder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

在视图中

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

然后在 app/helpers/application_helper.rb 中

and then in app/helpers/application_helper.rb

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

然而,这在form_for"中给了我一个错误

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

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

我哪里做错了?

推荐答案

try with :

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

这篇关于尝试扩展 ActionView::Helpers::FormBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:40