简体   繁体   中英

ruby rails tutorial css

Folks, I've been following a ruby tutorial, at a step where I am trying to add some CSS to the pages.

ActionController::RoutingError (No route matches [GET] "/assets/blueprint/print.css"):

I believe my routes.rb file is messed up:

FirstApp::Application.routes.draw do
  get "pages/home"
  get "pages/contact"
  get "pages/about"
  root :to => "home#index"

application.html.erb file:

<!DOCTYPE html>
<html>
<head>
    <title><%= title %></title>


    <%= csrf_meta_tags %>
    <%= stylesheet_link_tag 'blueprint/screen', :media => 'screen' %>
    <%= stylesheet_link_tag 'blueprint/print', :media => 'print' %>
</head>
<body>

<%= yield %>

</body>
</html>

Directory structure on the server:

first_app% ls public 
404.html  422.html  500.html  downloads  favicon.ico  index.html.backup  master.zip  robots.txt  stylesheets

[vasiliy@vbweb]~/ruby/first_app% ls public/stylesheets 
blueprint

[vasiliy@vbweb]~/ruby/first_app% ls public/stylesheets/blueprint 
ie.css  plugins  print.css  screen.css  src

You should check the tutorial you're following, but it seems you following a slightly older tutorial (before the asset pipeline was added in Rails 3.1)

So things are a little different.

Stylesheets now are expected to live in app/assets/stylesheets

If you just move blueprint/screen.css and blueprint/print.css into that directory, it should start working.


But... you may want to look at how the asset pipeline works, as it's quite cool.

Take a look at application.css and you'll see a few lines like

/*
*= require_tree .
*= require_self
*/

This will require all files inside the stylesheets directory, you can thereby just have a single line stylesheet_link_tag :application which will do them all.

However, that won't do the print/screen media settings you're trying to play with.

I think your routes file is fine.

The stylesheet_link_tag references are a Rails "view helper" that gets you a valid path to an asset located under the assets/ directory. This allows you to take advantage of the Rails "Asset Pipeline" (worth googling), which will concatenate, minify, and compress the heck out of all your JS and CSS assets in production. In development, on the other hand, it'll leave them as individual assets so you can debug things.

The public/ folder doesn't contain Asset Pipeline assets - instead, these are just files that get shoved under the webroot. These can be referenced as usual:

<link rel="stylesheet" href="/blueprint/print">

However, you almost certainly want to put them into the app/assets directory, instead, so they are handled properly in production. I'm guessing the stylesheet_link_tag used to reference href='application', right? So put your css under assets/stylesheets, and double-check that assets/stylesheets/application.css does 'require_tree' or similar. Then, if you get rid of these stylesheet_link_tag references and replace them with the original one, you should be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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