我如何使用Bootstrap表单选择css与Rails模型? [英] How do I use the Bootstrap form select css with a Rails model?
问题描述
我想在Ruby on Rails应用程序中使用Bootstrap css在表单中设置一个选择框。
I want to use the Bootstrap css to style a "select" box in a form in a Ruby on Rails app.
在Bootstrap网站上,以此为例:
On the Bootstrap site, they give this as an example:
<select class="form-control">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
但是,我不知道如何结合这个方法与我的ruby on rails code for select区域,以便它将选定的选项保存到我的表中正确的列(:ampm here)。这是我目前的代码。它工作,但没有我想要的引导看起来:
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:
<%= f.label :am_or_pm %>
<div class="form-control">
<%= f.select(:ampm, options_for_select([['AM', 1], ['PM', 2]])) %>
</div>
我已经尝试了很多方法来整合Bootstrap示例和我的代码,但没有什么可以工作。
I have tried a number of ways to integrate the Bootstrap example with my code, but nothing will work. Any help is appreciated.
推荐答案
首先,对于引导 form-control
class to work on select您必须将其添加到选择
标记本身
First of all, for bootstrap form-control
class to work on select you must add it to select
tag itself
Per apidoc rails形式选择
Per apidoc of rails form select
select(object, method, choices = nil, options = {}, html_options = {}, &block)
您可以将html类添加为
you can add html class as
<%= f.select(:ampm, options_for_select([['AM', 1], ['PM', 2]]), {}, {class: "form-control"}) %>
或简单
<%= f.select :ampm, [['AM', 1], ['PM', 2]], {}, {class: "form-control"}) %>
这篇关于我如何使用Bootstrap表单选择css与Rails模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!