Ruby-on-rails Rails button_to - 将 css 类应用于按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14123884/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:15:32  来源:igfitidea点击:

Rails button_to - applying css class to button

ruby-on-railsformserbbutton-to

提问by ezuk

This is silly, but I just can't figure out how to style the input element created by Rails button_to-- and yes, I've read the docs over and over again and tried the samples.

这很愚蠢,但我就是不知道如何设置由 Rails 创建的输入元素的样式button_to——是的,我一遍又一遍地阅读文档并尝试了示例。

Here's my ERB code:

这是我的 ERB 代码:

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

And this yield the following HTML:

这会产生以下 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>

Whereas all I want is just inputto have class = "btn btn-small".

而我想要的只是input拥有class = "btn btn-small".

Help? Thank you!

帮助?谢谢!

回答by Baldrick

The signature of the method button_tois

该方法的签名button_to

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

So use {}to distinguish optionsand html_options

所以{}用来区分optionshtml_options

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

回答by My Mai

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

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

form_class: class for the form

form_class:表单的类

class : class for button in the form

class : 表单中按钮的类