资产管道在我的 Rails 4.2 应用程序中的“application.scss"中不起作用 [英] Assets pipeline doesn't work in 'application.scss' in my Rails 4.2 app

查看:44
本文介绍了资产管道在我的 Rails 4.2 应用程序中的“application.scss"中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Rails 新手,如果这是一个愚蠢的问题,我深表歉意.但我无法解决我的问题:我将 Bootstrap 添加到我的新应用程序中,但它仍然没有使用任何新样式.

I'm new in Rails and apologize if that is a stupid question. But i cannot resolve my problem: I added Bootstrap to my new app but it still doesn't use any new styles.

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

I've renamed application.css to application.scss and created such structure:

|-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 样式都没有使用它们.

I've tried to write any style rules in article.scss but my app doesn't use them, so as any Bootstrap styles.

推荐答案

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

After several hours i've found a problem - my pages_controller was incorrect:

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

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

Controllers should inherit from main Controller!

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

P.S: Adding string layout "application" to the wrong controller also helped:

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

但我不建议那样走

这篇关于资产管道在我的 Rails 4.2 应用程序中的“application.scss"中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆