简体   繁体   English

Rails 4,Bootstrap 3,simple_form - 表单样式不起作用

[英]Rails 4, Bootstrap 3, simple_form - form styling not working

In the past I have used rails 3.x bootstrap 2.x and simple_form to great success. 在过去,我使用rails 3.x bootstrap 2.x和simple_form取得了巨大的成功。 However, I have tried to created a new (not upgraded) Rails 4 / Bootstrap 3 app and although the simple_form syntax is able to be used and can even use nested forms, it looks as though the styling is not working. 但是,我尝试创建一个新的(未升级的)Rails 4 / Bootstrap 3应用程序,虽然simple_form语法能够使用,甚至可以使用嵌套表单,但看起来好像样式不起作用。 I have used rails-bootstrap-forms on a none model based form and that styling looks fine, however, I would prefer to use simple_form for the majority of my forms. 我在基于非模型的表单上使用了rails-bootstrap-forms,并且样式看起来很好,但是,我更喜欢在我的大多数表单中使用simple_form。

My files look like the following 我的文件如下所示

gem 宝石

source 'https://rubygems.org'

gem 'rails', '4.1.6'
gem 'mysql2'
gem 'jquery-rails'
gem 'devise'

gem 'sass-rails'
gem 'coffee-rails', '~> 4.1.0'
gem 'bootstrap-sass', '~> 3.2.0'
gem 'autoprefixer-rails'
gem 'therubyracer', '0.11.4', :platforms => :ruby
gem 'uglifier', '>= 1.3.0'
gem 'jquery-ui-rails'
gem 'less-rails'
gem 'font-awesome-rails'
gem 'bootstrap_form'

gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0',          group: :doc

gem 'jquery-turbolinks'
gem 'turbolinks', '1.3.0'

#Forms and user entry
gem 'simple_form'
gem 'cocoon'
gem 'country_select'
gem "ckeditor"
gem "gon"

application.css.scss application.css.scss

/*
 *= require_self
 *= require rails_bootstrap_forms
 *= require font-awesome
 *= require_tree .
 */

@import "bootstrap-sprockets";
@import "bootstrap";

I have the two initializers that have not been altered simple_form.rb and simple_form_bootstrap.rb 我有两个没有被改变的初始化器simple_form.rb和simple_form_bootstrap.rb

and a form looking like the following 和表格如下所示

<%= simple_form_for @item, :html => { :class => 'form-horizontal' } do |f| %>

  <% if f.error_notification %>
    <div class="alert alert-error fade in">
      <a class="close" data-dismiss="alert" href="#">&times;</a>
      <%= f.error_notification %>
    </div>
  <% end %>

  <%= f.input :user_id, :as => :hidden, :input_html => { :value => @userid }  %>
  <%= f.association :category %>
  <%= f.input :name %>
  <%= f.input :description %>

  <%= f.input :cool %>
  <%= f.input :cold %>


  <%= f.button :submit, :class => 'btn-primary' %>
  <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
                items_path, :class => 'btn btn-default' %>
<% end %>

which outputs in the following way 以下列方式输出

      <div class="control-group hidden item_user_id"><div class="controls"><input class="hidden" id="item_user_id" name="item[user_id]" type="hidden" /></div></div>
      <div class="control-group select optional item_category"><label class="select optional control-label" for="item_category_id">Category</label><div class="controls"><select class="select optional" id="item_category_id" name="item[category_id]"><option value=""></option>
<option selected="selected" value="1">Option 1</option>
<option value="3">Option 2</option>
<option value="4">Option 3</option>
      <div class="control-group string optional item_name"><label class="string optional control-label" for="item_name">Name</label><div class="controls"><input class="string optional" id="item_name" name="item[name]" type="text" value="Underwear" /></div></div>
      <div class="control-group string optional item_description"><label class="string optional control-label" for="item_description">Description</label><div class="controls"><input class="string optional" id="item_description" name="item[description]" type="text" value="test" /></div></div>
      <div class="control-group boolean optional item_warm"><label class="boolean optional control-label" for="item_warm">Warm</label><div class="controls"><input name="item[warm]" type="hidden" value="0" /><label class="checkbox"><input checked="checked" class="boolean optional" id="item_warm" name="item[warm]" type="checkbox" value="1" /></label></div></div>
      <div class="control-group boolean optional item_cool"><label class="boolean optional control-label" for="item_cool">Cool</label><div class="controls"><input name="item[cool]" type="hidden" value="0" /><label class="checkbox"><input checked="checked" class="boolean optional" id="item_cool" name="item[cool]" type="checkbox" value="1" /></label></div></div>

In particular the checkboxes look really bad as shown below 特别是复选框看起来非常糟糕,如下所示

在此输入图像描述

Has anyone had any similar issues, or am I just doing something a bit dumb? 有没有人有任何类似的问题,或者我只是做一些有点愚蠢的事情?

any thoughts gratefully accepted 任何想法都感激不尽

You are right regarding the version in the Gemfile, also I found that forms renders better with this syntax : 你对Gemfile中的版本是正确的,我也发现使用这种语法表单更好:

<%= simple_form_for @item, html: {class: 'form-horizontal'},
                    wrapper: :horizontal_form,
                    wrapper_mappings: {
                            check_boxes: :horizontal_radio_and_checkboxes,
                            radio_buttons: :horizontal_radio_and_checkboxes,
                            file: :horizontal_file_input,
                            boolean: :horizontal_boolean
                    } do |f| %>

    <%= f.input :sample_model_field%>
    <div class="form-group">
      <div class="col-sm-offset-3 col-sm-9">
        <%= f.button :submit %>
      </div>
    </div>
<% end %>

You can also look at the sample app code : 您还可以查看示例应用代码:

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

Looks like the default version of simple_form is not completely working with Bootstrap 3 . 看起来simple_form的默认版本不能与Bootstrap 3完全兼容。 I therefore had to change my gem file to read the following: 因此,我不得不更改我的gem文件以阅读以下内容:

gem 'simple_form', '~> 3.1.0.rc1', github: 'plataformatec/simple_form', branch: 'master' 

This has resolved the issue once I reran the initializer and restarted the app. 一旦我重新启动初始化程序并重新启动应用程序,这就解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM