Twitter-Bootstrap 和 Rails 中的表单
这可能是一个初学者的问题,但没有太多运气能够解决这个问题。背景:
- Rails 3.0.x 版本
- Bootstrap 2.0 - 使用预编译,简单地放入公共/样式表中。 Att
- 尝试做一个水平布局的表单,即标签位于同一行的输入字段旁边。
问题是我无法使表单正常工作,但可以以相同的表单获取默认的 Bootstrap 示例代码以正确布局。 Rails 表单如下所示:
<div class="row-fluid">
<div class="span4 well">
<%= form_for @member, :html => {:class => "form-horizontal"} do |m| %>
<fieldset>
<%= m.label :title %>
<%= m.text_field :title %>
<%= m.label :first_name %>
<%= m.text_field :first_name %>
<% end %>
</div>
</div>
请注意,form_for 方法具有 form-horizontal 类,如 Bootsrap form CSS
当显示标签时,例如标题,在一行上向左调整,然后在下一行是输入字段,也向左调整。
现在,如果我包含一些表单的示例引导程序代码,例如:
<div>
<form class="form-horizontal">
<fieldset>
<legend>Legend text</legend>
<div class="control-group">
<label class="control-label" for="input01">Text input</label>
<div class="controls">
<input type="text" class="input-xlarge" id="input01">
<p class="help-block">Supporting help text</p>
</div>
</div>
</fieldset>
</form>
</div>
然后我会得到预期的结果 - 标签和输入字段位于水平布局的同一行上。
我尝试将样式“control-label”添加到 m.label 和 m.text_field 组件上,但对将布局设置为水平没有任何乐趣。
因此,不确定问题是什么,因为工作部分验证了我可以让 Bootstrap 工作,尽管我的 Rails 表单不支持表单水平。
This may be a beginners question but not having much luck getting this going. The background:
- Rails 3.0.x version
- Bootstrap 2.0 - using as pre-compiled simply placed into public/stylesheets.
Att - Attempting to do a form with horizontal layout, i.e. label next to the input field on the same line.
The issue is that I can't get the form to work, but can get the default Bootstrap sample code, in the same form to layout correctly. The Rails form looks like:
<div class="row-fluid">
<div class="span4 well">
<%= form_for @member, :html => {:class => "form-horizontal"} do |m| %>
<fieldset>
<%= m.label :title %>
<%= m.text_field :title %>
<%= m.label :first_name %>
<%= m.text_field :first_name %>
<% end %>
</div>
</div>
Notice that the form_for method has the class of form-horizontal as described by in Bootsrap form CSS
When displayed the label, for example title, is on one line left adjusted and then on the next line is the input field, also left adjusted.
Now if I include some of the sample Bootstrap code for forms such as:
<div>
<form class="form-horizontal">
<fieldset>
<legend>Legend text</legend>
<div class="control-group">
<label class="control-label" for="input01">Text input</label>
<div class="controls">
<input type="text" class="input-xlarge" id="input01">
<p class="help-block">Supporting help text</p>
</div>
</div>
</fieldset>
</form>
</div>
Then I get the expected outcome -- label and input field on the same line in horizontal layout.
I've tried adding the style "control-label" onto the m.label and m.text_field components but no joy in getting the layout as horizontal.
Thus, not sure what the issue is, since the working part validates that I can get Bootstrap to work, although with my Rails Form it's not honouring the form-horizontal.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想要使用普通表单(例如,没有 simpleform-bootstrap 或其他 gem),则需要相应地调整表单 HTML:
正如 Arun Kumar Arjunan 所说,表单构建器不会生成 bootstrap 框架所需的 html。
一定要检查 bootstrap 示例页面 中所需的 HTML,和/或通过检查 DOM(我发现它并没有很好的记录)。总而言之,您可以像上面一样通过创建自己的表单生成器来手动完成此操作,或使用各种宝石。
另外,请务必查看如何自定义错误 css< /a>,因为默认情况下,当发生验证错误时,该字段会被
包裹,从而破坏所使用的 css 选择器通过引导程序。
If you want to do the form vanilla (as in, without simpleform-bootstrap or other gems), you need to shape the form HTML accordingly:
As Arun Kumar Arjunan says, the form builder is not generating the html needed for the bootstrap framework.
Definitely check out the HTML needed in the bootstrap example pages, and/or by inspecting the DOM (i found that it's not really well documented). All in all, you can do it manually, like above, by creating your own form builder, or by using various gems.
Also as a sidenote, make sure to check out how to customize the errors css, because by default, when a validation error occurs, the field is wrapped by a
<div class ="field_with_errors">
breaking the css selectors used by bootstrap.问题是字段助手没有为 twitter bootstrap 生成正确的 html。
您可以使用支持 twitter bootstrap 的 simple_form gem。请参阅文档:
https://github.com/plataformatec/simple_form
以下是示例应用程序:https://github.com/rafaelfranca/simple_form-bootstrap
直播演示:http://simple-form-bootstrap.plataformatec.com.br/
The problem is the field helpers are not generating the proper html for twitter bootstrap.
You can use simple_form gem which supports twitter bootstrap. See the documentation:
https://github.com/plataformatec/simple_form
Here is the example application: https://github.com/rafaelfranca/simple_form-bootstrap
Live Demo: http://simple-form-bootstrap.plataformatec.com.br/
我发现 Stephen Potenza 的 bootstrap-form gem 非常有用(并且易于使用)。将 gem 添加到您的 gemfile 中,并在您将使用“form_for”的地方使用“bootstrap_form_for”:
使整个事情变得轻而易举。
https://github.com/potenza/bootstrap_form
I've found Stephen Potenza's bootstrap-form gem to be incredibly helpful (and easy to use). Add the gem to your gemfile, and use 'bootstrap_form_for' where you would use 'form_for':
Makes the whole thing a breeze.
https://github.com/potenza/bootstrap_form
另一种方法可能更简单:
在您的
views//edit.html.erb
中:然后,创建一个像
views//_form 这样的文件。 html.erb
将包含启用 Bootstrap 的表单字段,即:Another approach that might be a bit simpler:
In your
views/<model>/edit.html.erb
:Then, create a file like this
views/<model>/_form.html.erb
which will contain your Bootstrap-enabled form fields, i.e.: