RoR 3 - 将FormBuilder的默认f.submit重写为一个按钮

时间:2010-09-29 12:17:53

标签: ruby-on-rails ruby-on-rails-3 formbuilder formhelper

我有一个表单,对于布局(以及将来使用),我想知道如何更改生成的默认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: 

1 个答案:

答案 0 :(得分:3)

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

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

中添加此自动加载目录