Twitter-Bootstrap:Simple_form没有制作横向格式或正确的HTML输出?

Lea*_*RoR 11 ruby-on-rails ruby-on-rails-3 simple-form ruby-on-rails-3.1

我不知道为什么它不像示例那样重复.当我把以下代码用于这个表单时:

<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :class => "form-horizontal" } ) do |f| %>
<%= f.input :user_name, :input_html => { :class => "span3" }, :hint => "just letters and numbers please." %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

现在它看起来像这样:

在此输入图像描述

当我希望它像这样(这里的第一个例子:http://simple-form-b​​ootstrap.plataformatec.com.br/articles/new).

在此输入图像描述

问题在于生成的HTML.我的HTML是:

<div class="input string optional">
 <label for="user_user_name" class="string optional"> User name</label>
  <input type="text" size="50" name="user[user_name]" maxlength="25" id="user_user_name" class="string optional span3">
   <span class="hint">no spaces or special characters, just letters and numbers please</span>
</div>
Run Code Online (Sandbox Code Playgroud)

和Simple_forms HTML:

<div class="control-group string required">
 <label for="article_name" class="string required">
  <abbr title="required">*</abbr> Name
 </label>
  <div class="controls">
   <input type="text" size="50" name="article[name]" id="article_name" class="string required span6">
    <p class="help-block">add your article title here</p>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

完全不同.我在想Bootstrap生成器不生成?你怎么看?我该怎么办?

资源

https://github.com/plataformatec/simple_form

https://github.com/rafaelfranca/simple_form-b​​ootstrap

http://simple-form-b​​ootstrap.plataformatec.com.br/

ahm*_*eij 10

您是否为simple_form添加了初始化程序?此初始化程序设置应该用于输出引导程序html的包装器

rails generate simple_form:install --bootstrap
Run Code Online (Sandbox Code Playgroud)

请注意,这仅适用于simple_form 2!

  • 该死的,我现在明白了.bundle install转到`1.5.2`,所以我必须手动使用`gem"simple_form","〜> 2.0.0.rc"`.我在一开始就没有看到"这些说明适用于2.0 ......".如果你直接进入Bootstrap部分,很难看出来.谢谢ahmeij. (5认同)

All*_*len 10

的输出

rails g simple_form:install --bootstrap
Run Code Online (Sandbox Code Playgroud)

进一步说明:

Inside your views, use the 'simple_form_for' with one of the Bootstrap form
classes, '.form-horizontal', '.form-inline', '.form-search' or
'.form-vertical', as the following:

  = simple_form_for(@user, :html => {:class => 'form-horizontal' }) do |form|
Run Code Online (Sandbox Code Playgroud)

因此,您必须将该:html => {:class => 'form-horizontal' }选项添加到_form.html要更改表单样式的每个文件中.您可以使用'form-search','form-inline','form-horizo​​ntal'或'form-vertical'(默认值).

要将默认表单设置为水平,请编辑

lib/templates/erb/scaffold/_form.html.erb
Run Code Online (Sandbox Code Playgroud)

并将第一行更改为此(使用您首选的表单类名称):

<%%= simple_form_for(@<%= singular_table_name %>, :html => {:class => 'form-horizontal' } ) do |f| %>
Run Code Online (Sandbox Code Playgroud)

对于那些使用HAML的人来说,文件路径和格式会有所不同.