RoR 3 - 覆盖 FormBuilder 默认的 f.submit 到按钮中

发布于 2024-09-25 05:53:32 字数 1349 浏览 3 评论 0原文

我有一个表单,为了布局(和将来的使用),我想知道如何更改生成的默认 f.submit:

到一个不应该给出任何错误的 html 标签。

我现在拥有的是 formbuilder 上的扩展

在我看来:

   <%= form_for resource, :as => resource_name, :url => session_path(resource_name),  :class => "form with-margin", :builder => AppFormBuilder do |f| %>
   ...
   <%= f.submit %>
   <% end %>

在我的 lib/appformbuilder 中:

class AppFormBuilder < ActionView::Helpers::FormBuilder
  def submit(text, options = {})
    options[:type] = "submit"
    @template.content_tag(:button, text, options)
  end
end

但这给了我一个错误:

NameError in Devise/sessions#new

Showing d:/Projects/Websites/Ruby On Rails/fact-it/app/views/devise/sessions/new.html.erb where line #11 raised:
uninitialized constant ActionView::CompiledTemplates::AppFormBuilder


8:         
9:       <p class="message error no-margin alert"><%= alert %></p>
10:       <p class="notice"><%= notice %></p>
11:       <%= form_for resource, :as => resource_name, :url => session_path(resource_name), :class => "form with-margin", :builder => AppFormBuilder do |f| %>
12:         <p><%= f.label :email %><br />
13:         <%= f.text_field :email %></p>
14: 

I have a form, and for layout (and future use), i would like to know how to change the default f.submit that generates a:

To a html tag that shouldn't give any errors.

What i have now is an extention on the formbuilder

In my view:

   <%= form_for resource, :as => resource_name, :url => session_path(resource_name),  :class => "form with-margin", :builder => AppFormBuilder do |f| %>
   ...
   <%= f.submit %>
   <% end %>

In my lib/appformbuilder:

class AppFormBuilder < ActionView::Helpers::FormBuilder
  def submit(text, options = {})
    options[:type] = "submit"
    @template.content_tag(:button, text, options)
  end
end

But this gives me an error:

NameError in Devise/sessions#new

Showing d:/Projects/Websites/Ruby On Rails/fact-it/app/views/devise/sessions/new.html.erb where line #11 raised:
uninitialized constant ActionView::CompiledTemplates::AppFormBuilder


8:         
9:       <p class="message error no-margin alert"><%= alert %></p>
10:       <p class="notice"><%= notice %></p>
11:       <%= form_for resource, :as => resource_name, :url => session_path(resource_name), :class => "form with-margin", :builder => AppFormBuilder do |f| %>
12:         <p><%= f.label :email %><br />
13:         <%= f.text_field :email %></p>
14: 

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

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

发布评论

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

评论(1

痴情 2024-10-02 05:53:32

如果您想使用 Rails 自动加载,则需要将此变量放入 lib/app_form_builder.rb 中。

警告,在 Rails 3 中,这种默认自动加载不再存在。您需要在 application.rb 中添加此自动加载目录

You need put this variable in lib/app_form_builder.rb if you want use the rails autoloading.

Warning, in Rails 3 this default autoloading doesn't exist anymore. You need add this autoload directory in your application.rb

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