Ruby on rails 如何实现一个按钮标签来为helper形成_?

Ruby on rails 如何实现一个按钮标签来为helper形成_?,ruby-on-rails,ruby-on-rails-3,forms,helper,Ruby On Rails,Ruby On Rails 3,Forms,Helper,我需要实现一个创建…标记的帮助程序,我需要执行类似的操作: <%= form_for(some_var) do |f| %> <%= f.submit '+' %> <% end %> 助手的工作方式应如下所示: <%= f.button '+' %> # Returns <button type="submit">+</button> def submit_button(object) image

我需要实现一个创建
标记的帮助程序,我需要执行类似的操作:

<%= form_for(some_var) do |f| %>
  <%= f.submit '+' %>
<% end %>

助手的工作方式应如下所示:

<%= f.button '+' %>
# Returns
<button type="submit">+</button>
def submit_button(object)
    image   = "#{image_tag('/images/icons/tick.png', :alt => '')}"

    if object.is_a?(String)
      value = "#{image}#{object}"
    else
      name  = object.class.to_s.titlecase
      value = object.new_record? ? "#{image} Save #{name} Information" : "#{image} Update #{name} Information"
    end

    content_tag :button, :type => :submit, :class => 'button positive' do
      content_tag(:image, '/images/icons/tick.png', :alt => '')
      value
    end
  end

#返回
+
我看到了,但是Rails 3.0.7中没有实现


我需要做什么才能在我的应用程序中实现这个帮助程序?

我之前在我的一个应用程序中实现了一个类似的帮助程序方法。我需要一个按钮标签,按钮上有一个图像和一个自己的类。您可以传递按钮上显示的文本字符串或对象本身。看起来是这样的:

<%= f.button '+' %>
# Returns
<button type="submit">+</button>
def submit_button(object)
    image   = "#{image_tag('/images/icons/tick.png', :alt => '')}"

    if object.is_a?(String)
      value = "#{image}#{object}"
    else
      name  = object.class.to_s.titlecase
      value = object.new_record? ? "#{image} Save #{name} Information" : "#{image} Update #{name} Information"
    end

    content_tag :button, :type => :submit, :class => 'button positive' do
      content_tag(:image, '/images/icons/tick.png', :alt => '')
      value
    end
  end
然后以

看起来是这样的:

<%= f.button '+' %>
# Returns
<button type="submit">+</button>
def submit_button(object)
    image   = "#{image_tag('/images/icons/tick.png', :alt => '')}"

    if object.is_a?(String)
      value = "#{image}#{object}"
    else
      name  = object.class.to_s.titlecase
      value = object.new_record? ? "#{image} Save #{name} Information" : "#{image} Update #{name} Information"
    end

    content_tag :button, :type => :submit, :class => 'button positive' do
      content_tag(:image, '/images/icons/tick.png', :alt => '')
      value
    end
  end

您可以创建自定义表单帮助器,该帮助器继承自FormBuilder,用于创建表单。我创建了这个按钮方法用于Twitter的引导

用任何合适的方式替换“引导”。(也许是CuteAsAButtonBuilder?)

app/helpers/bootstrap\u form\u builder.rb

打电话给魔术师

<%= bootstrap_form_for @person do |form| %>
  <%= form.button 'Click Me' %>
<% end %>


我认为使用助手更明智。您的解决方案可行,但我希望像
f.button
这样的工具看起来像其他输入。我可以在helpers中复制Rails 3.1 button_标记并使用它,但我认为使用
f.button
有助于使代码更清晰。@jrdi是的,您可以始终覆盖默认实现。前提是您记住您已经覆盖并确保它不会破坏任何其他内容,并将其传达给团队。或者,采用这种方法,实现一种完全不同的方法,并使用它。没有麻烦。只是有完全相同的用例!非常有用:)一个很小的错误修复:按钮方法定义应该是def button(label,options={}),否则options[:class]总是非常正确。刚刚修好了。我不小心忘了
options=args.extract\u options
这同样可以实现这个功能,但是哈希看起来非常漂亮。