Rails button_to - 将css类应用于按钮

时间:2013-01-02 14:35:23

标签: ruby-on-rails forms erb button-to

这很愚蠢,但我无法弄清楚如何设置由Rails button_to创建的输入元素的样式 - 是的,我一遍又一遍地阅读文档并尝试了样本。 / p>

这是我的ERB代码:

 <%= button_to "Claim", action: "claim", idea_id: idea.id, remote: true, class: 'btn btn-small'  %>

这会产生以下HTML:

<form action="/ideas/claim?class=btn+btn-small&amp;idea_id=4&amp;remote=true" class="button_to" method="post">
  <div>
    <input data-remote="true" type="submit" value="Claim">
    <input name="authenticity_token" type="hidden" value="pU3umgqrDx3WbalfNK10c9H5B5N4OzPpohs4bWW8mow=">
  </div>
</form>

尽管我想要的只是input才有class = "btn btn-small"

帮助?谢谢!

2 个答案:

答案 0 :(得分:40)

方法button_to的签名是

button_to(name = nil, options = nil, html_options = nil, &block)

因此,请使用{}来区分optionshtml_options

<%= button_to "Claim", {action: "claim", idea_id: idea.id, remote: true}, {class: 'btn btn-small'} %>

答案 1 :(得分:10)

<%= button_to "Claim", {action: "claim", idea_id: idea.id, remote: true}, {form_class: 'form', class: 'btn btn-small'} %>

form_class:表单的类

class:表单中的按钮类