2015-05-23 26 views
1

我想用simple_form适应引导代码适应引导到simple_form

<div class="row"> 
       <div class="form-group col-lg-6"> 
        <label>Nick</label> 
        <input type="text" class="form-control"> 
       </div> 
       <div class="form-group col-lg-6"> 
        <label>Email</label> 
        <input type="email" class="form-control"> 
       </div> 
</div> 

我想是这样

<%= simple_form_for User.new do |f| %> 
    <%= f.input :nick, label: "Nick", class: 'form-group col-lg-6' %> 
    <%= f.input :email, label: "Email", class: 'form-group col-lg-6'%> 
<% end %> 

但没有成功。有人可以帮我解决吗?

回答

0

Simpleform需要一些配置才能很好地与bootstrap配合使用。

创建初始化程序和配置simpleform与引导工作:

# File here config/initializers/simple_form_bootstrap.rb 
SimpleForm.setup do |config| 
    config.wrappers :vertical_input_group, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| 
    b.use :html5 
    b.use :placeholder 
    b.use :label, class: 'control-label' 

    b.wrapper tag: 'div' do |ba| 
     ba.wrapper tag: 'div', class: 'input-group col-sm-12' do |append| 
     append.use :input, class: 'form-control' 
     end 
     ba.use :error, wrap_with: { tag: 'span', class: 'help-block' } 
     ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' } 
    end 
    end 

    config.wrappers :horizontal_input_group, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| 
    b.use :html5 
    b.use :placeholder 
    b.use :label, class: 'col-sm-3 control-label' 

    b.wrapper tag: 'div', class: 'col-sm-9' do |ba| 
     ba.wrapper tag: 'div', class: 'input-group col-sm-12' do |append| 
     append.use :input, class: 'form-control' 
     end 
     ba.use :error, wrap_with: { tag: 'span', class: 'help-block' } 
     ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' } 
    end 
    end 
end 

更多信息可以在这里找到:https://github.com/plataformatec/simple_form/wiki/How-to-use-Bootstrap-3-input-group-in-Simple-Form

0

你不应该说User.new直接进入视野,在其控制器做到这一点, 你的情况的用户控制器,这样说:@user = User.new

,并在您的意见

<%= simple_form_for @user, html: { multipart: true } do |f| %> 
    <%= f.input :nick, label: "Nick", class: 'form-group col-lg-6' %> #i presume :nick is your tables name,instead of saying :name. 
    <%= f.input :email, label: "Email", class: 'form-group col-lg-6'%> 
<% end %> 
+0

但我不会在用户 – adolzi

+1

中调用这个simple_form您不必在用户控制器中调用simple_form,它的视图就足够了.Juts在您使用的控制器中调用@user = User.new的关系。 –

+0

和@user在其视图中 –