Ruby-on-rails 你如何覆盖 form_for 助手中的类名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6881645/
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
How do you override the class name in the form_for helper?
提问by James
I need to add some class names to my form. I'm using Rails' form_forhelper to do this. I've tried adding { :class => 'classname' }to no avail.
我需要在表单中添加一些类名。我正在使用 Rails 的form_for助手来做到这一点。我试过添加{ :class => 'classname' }无济于事。
How can I add classes to this Rails helper output?
如何向此 Rails 帮助程序输出添加类?
回答by Harish Shetty
Try this:
尝试这个:
form_for @order, :html => {:class => "foo"}
回答by Daniel
For Rails 4.0 here is the example from the guides:
对于 Rails 4.0,这里是指南中的示例:
http://guides.rubyonrails.org/form_helpers.html
http://guides.rubyonrails.org/form_helpers.html
1.2 Multiple Hashes in Form Helper Calls
1.2 表单助手调用中的多个哈希
form_tag(controller: "people", action: "search", method: "get", class: "nifty_form")
# => '<form accept-charset="UTF-8" action="/people/search?method=get&class=nifty_form" method="post">'
回答by Lorem Ipsum Dolor
Ruby Code Below,
下面的 Ruby 代码,
<%= form_for :article, :html => { :class => "article-form" } do |form| %>
<p>
<%= form.label :title %>
<%=
form.text_field :title,
placeholder: "A******'s title"
%>
</p>
<p>
<%= form.label :text %>
<%=
form.text_field :text,
placeholder: "A******'s content"
%>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
HTML Output Below,
下面的 HTML 输出,
<form class="article-form" action="/articles/new" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="?"><input type="hidden" name="authenticity_token" value="BRBjb0maKb5kNbGXV0wwoLeFUV6El2la/sOwweWhyN+nEIOK2Wam5Xz8PasZtpwvmiCF7yIO8Pab/OR2uoeZQA==">
<p>
<label for="article_title">Title</label>
<input placeholder="A******'s title" type="text" name="article[title]" id="article_title">
</p>
<p>
<label for="article_text">Text</label>
<input placeholder="A******'s content" type="text" name="article[text]" id="article_text">
</p>
<p>
<input type="submit" name="commit" value="Save Article">
</p>
</form>

