资产管道不适用于application.scss'在我的Rails 4.2应用程序中

时间:2015-02-28 15:33:14

标签: ruby-on-rails twitter-bootstrap sass ruby-on-rails-4.2 ruby-2.2

我是Rails中的新手并且如果这是一个愚蠢的问题而道歉。但我无法解决我的问题:我添加了Bootstrap到我的新应用程序,但它仍然没有使用任何新的样式。

我已将application.css重命名为application.scss并创建了这样的结构:

|-stylesheets
   |-elements
   |--articles
   |---article.scss
   |-application.scss

Application.scss

/* User styles
* Plugins first
 */
@import "bootstrap-sprockets";
@import "bootstrap";

@import "elements/**/*";

/*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
 * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
 *
 * You're free to add application-wide styles to this file and they'll appear at the bottom of the
 * compiled file so the styles you add here take precedence over styles defined in any styles
 * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
 * file per style scope.
 *
 *= require_self
 */

在Gem档案中,我添加了

source 'https://rubygems.org'
ruby '2.2.0'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2'

# Установка Bootstrap
gem 'sass-rails'
gem 'bootstrap-sass'
#Autoprefixer optional, but recommended. It automatically adds the proper vendor prefixes to your CSS code when it is compiled.
gem 'autoprefixer-rails'

的application.js

//= require jquery
//= require jquery_ujs
//= require bootstrap-sprockets
//= require_tree .

我已尝试在article.scss中编写任何样式规则,但我的应用程序并未使用它们,因此任何Bootstrap样式都可以使用。

3 个答案:

答案 0 :(得分:2)

有一种更简单的方法,我将描述序列: 在添加gem 'bootstrap-sass' bundle install后,重新启动服务器 而不是添加config/application.rb下一个字符串:

config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif)

为您的图片,而不是在custom.css.scss中创建app/assets/stylesheets并添加@import "bootstrap";, 在你的custom.css.scss中,你可以自己编写风格

答案 1 :(得分:1)

我怀疑你的环境中有旧版本的bootstrap。尝试使用gem 'bootstrap-sass', '3.3.3'更新您的Gemfile并运行bundle。确保在更新后重新启动服务器。

或者,您可以运行bundle update

答案 2 :(得分:1)

几个小时后,我发现了一个问题 - 我的pages_controller不正确:

class PagesController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
end

而不是:

class PagesController < ApplicationController
  def index_page
  end
end

控制器应该从主控制器继承!

P.S: 将字符串layout "application"添加到错误的控制器也有帮助:

class PagesController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  layout "application"
end

但我不建议那样走