0

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

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

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

应用程序.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'

应用程序.js

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

我尝试在 article.scss 中编写任何样式规则,但我的应用程序不使用它们,任何 Bootstrap 样式也是如此。

4

3 回答 3

2

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

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

对于您的图像,而不是custom.css.scss在其中创建app/assets/stylesheets和添加@import "bootstrap";custom.css.scss您可以编写自己的样式

于 2015-02-28T16:10:13.810 回答
1

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

或者,您可以只运行bundle update.

于 2015-02-28T16:13:44.780 回答
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

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

PS:将字符串添加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

但我不建议走那条路

于 2015-02-28T20:53:45.907 回答