简体   繁体   中英

ruby on rails and css issue

I am learning ruby on rails, but I have this problem with my CSS code.

so in ~/Ruby Code/My_Project/app/views/layouts/application.html.erb I have this:

<!DOCTYPE html>
<html>
<head>
  <title>MyProject</title>
  <%= stylesheet_link_tag    :all  %>
  <%= javascript_include_tag :defaults %>
  <%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
</html>

and in ~/Ruby Code/My_Project/public/stylesheets I have this, a CSS file called "My_Project.css".

also in ~/Ruby Code/My_Project/app/views/users, I have the index.html.erb file:

<div id="user_list" >

<h1>Listing users</h1>

<table>

    <% @users.each do |user| %>

    <tr class="<%= cycle('list_line_odd', 'list_line_even') %>" >

        <td class="user_description" >
            <dl>
            <dt><%= user.name %></dt>
            <dt><%= user.surname %></dt>
            <dt><%= user.age %></dt>
            <dt><%= user.date_birth %></dt>
            <dt><%= user.date_of_reg %></dt>
            <dt><%= user.email %></dt>
        </td>
        <td class="list_actions" >
            <%= link_to 'Show', user %><br/>
            <%= link_to 'Edit', edit_user_path(user) %><br/>
            <%= link_to 'Destroy', user,
            :confirm => 'Are you sure?',
            :method => :delete %>
        </td>

    </tr>
    <% end %>

</table>
</div>
<br />
<%= link_to 'New user', new_user_path %>

why is Ruby not loading the CSS file ? Is it placed in the wrong directory?

Since rails 3.1 there is a thing called the asset pipeline . This will make sure that your css files are packaged into 1 file, and one other file containing all your js files. This will make sure that the initial download is much quicker.

In your app/assets/stylesheets is an application.css which serves as the manifest. This file will contain a description which css files will need to be included in the complete application.css .

This file, by default, contains something like

/* 
 *= require_self
 *= require_tree .
 */

and this means that whatever is inside the application.css file itself, plus all other files in the same folder will be included in the final application.css .

Hope this helps.

2 issues:

  1. wrong path - don't put asset files directly into public. the asset pipeline compiles files from /app/assets/stylesheets and outputs them to /public/assets . So move your css file to /app/assets/stylesheets first.

  2. wrong filename - see the 5th line of your first code example where it says <%= stylesheet_link_tag :all %> ? That's telling the asset pipeline to look for a css file in /public/assets named all.css . Obviously your css file is not named that. So change that as well and you should be good to go!

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