twitter-bootstrap 如何在 Rails 模型中使用 Bootstrap 表单选择 css?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22111088/
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 I use the Bootstrap form select css with a Rails model?
提问by rryyaann22
I want to use the Bootstrap css to style a "select" box in a form in a Ruby on Rails app.
我想使用 Bootstrap css 在 Ruby on Rails 应用程序的表单中设置“选择”框的样式。
On the Bootstrap site, they give this as an example:
在 Bootstrap 网站上,他们以此为例:
<select class="form-control">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
However, I can not figure out how to combine this method with my ruby on rails code for the select area I want to do, so that it saves the selected option into the correct column (:ampm here) in my table. This is the code I have currently. It works, but does not have the bootstrap look I want:
但是,我无法弄清楚如何将此方法与我想要执行的选择区域的 ruby on rails 代码相结合,以便它将所选选项保存到我的表中的正确列(此处为:ampm)中。这是我目前拥有的代码。它有效,但没有我想要的引导程序外观:
<%= f.label :am_or_pm %>
<div class="form-control">
<%= f.select(:ampm, options_for_select([['AM', 1], ['PM', 2]])) %>
</div>
I have tried a number of ways to integrate the Bootstrap example with my code, but nothing will work. Any help is appreciated.
我尝试了多种方法将 Bootstrap 示例与我的代码集成,但没有任何效果。任何帮助表示赞赏。
回答by Rahul Singh
First of all, for bootstrap's form-controlclass to work, you must add it to selecttag itself
首先,要使引导程序的form-control类工作,您必须将其添加到select标签本身
Per apidocof rails form select
每个apidocof rails 表单选择
select(object, method, choices = nil, options = {}, html_options = {}, &block)
you can add html class as
您可以将 html 类添加为
<%= f.select(:ampm, options_for_select([['AM', 1], ['PM', 2]]), {}, {class: "form-control"}) %>
or simply
或者干脆
<%= f.select :ampm, [['AM', 1], ['PM', 2]], {}, {class: "form-control"}) %>
回答by Bhavnesh
This works for me:
这对我有用:
<%= f.select :secretquestion, options_for_select([
["Select One", ""],
"Your nick name",
"your schoool name",
"your love name",
"your comapnay name",
"your favourite website"]),
{}, { class: "form form-group form-control" } %>

