1

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?

4
  • 2
    If you're just starting to learn rails, you should use at least use 3.1. In Rails 3.1, stylesheets are no longer stored in public/stylesheets but use the asset pipeline and are stored under assets/stylesheets. Commented Dec 5, 2011 at 13:45
  • I just moved the css file over there, and it still doesnt work. Is maybe he filename wrong? Commented Dec 5, 2011 at 13:53
  • ok found the error. I had to change <%= stylesheet_link_tag :all %> <%= javascript_include_tag :defaults %> to <%= stylesheet_link_tag "application" %> <%= javascript_include_tag "application" %> Commented Dec 5, 2011 at 13:59
  • any idea why it works now ? lol, does "application" tag means to look in the assets folder? Commented Dec 5, 2011 at 13:59

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.