Ruby-on-rails 有没有办法使用 rails 表单助手来生成按钮标签以供提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2254720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way with rails form helper to produce a button tag for submit
提问by Jonathan
I am trying to create buttons ala Wufoo (Rediscovering the button element)
我正在尝试创建按钮 ala Wufoo(重新发现按钮元素)
I would like to write the following code like the following:
我想编写如下代码:
<%form_tag search_path, :method => :get, :class => 'search' do %>
<%=text_field_tag :search, params[:search] -%>
<%=button_tag 'search', :name => nil-%>
<%end%>
To generate the following HTML (instead of the input[type="submit"] tag)
生成以下 HTML(而不是 input[type="submit"] 标签)
<button type="submit" class="positive">
<img src="/images/icons/tick.png" alt=""/>
Save
</button>
Does a method exist already? Or should I roll my own helper?
方法是否已经存在?还是我应该推出自己的帮手?
回答by EmFi
You could use content_tag to achieve this. It's the more railsy way of doing what you want. But it's longer than the raw HTML.
您可以使用 content_tag 来实现这一点。这是做你想做的事情的更荒谬的方式。但它比原始 HTML 长。
<% content_tag :button :type => :submit, :class => :positive do %>
<%= image_tag "icons/tick.png"%>
Save
<% end %>
Which produces
其中产生
<button type="submit" class="positive">
<img src="/images/icons/tick.png" alt="Tick"/>
Save
</button>
However using this as a starting point you should have no problem rolling your own robust helper, or abstracting it to a partial.
但是,使用它作为起点,您应该可以毫无问题地滚动您自己的强大助手,或将其抽象为部分。
回答by meagar
You can use the image_submit_taghelper to create an image submit tag, rather than wrapping the whole thing in a button:
您可以使用image_submit_tag助手来创建图像提交标签,而不是将整个内容包装在一个按钮中:
<%
image_submit_tag("login.png")
# => <input src="/images/login.png" type="image" />
image_submit_tag("purchase.png", :disabled => true)
# => <input disabled="disabled" src="/images/purchase.png" type="image" />
image_submit_tag("search.png", :class => 'search-button')
# => <input class="search-button" src="/images/search.png" type="image" />
%>
This might not be what you're looking for, if you require the "Save" text to appear above the <img>
如果您需要“保存”文本出现在 <img>
回答by Travis Pessetto
Use The JQuery Cheats Gem https://github.com/plowdawg/jquery_cheatsand in your view it is just
使用 JQuery Cheats Gem https://github.com/plowdawg/jquery_cheats在你看来它只是
<%= submitimage("/path/to/image.png","Alternate Text") %>
NOTE:Alternate text is optional.
注意:替代文本是可选的。

