2015-05-23 28 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在其視圖中 –