Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign different stylesheets in a rails project?

I have a stylesheet, application.css defined in layouts/application.html.erb:

<%= stylesheet_link_tag "application" %>

However, there's a section of the site where the views will use a completely different stylesheet, dashboard.css which I've defined in its index.html.erb:

<head>
    <title>My title</title>
    <%= stylesheet_link_tag "dashboard" %>
..

Unless I remove the stylesheet_link_tag in the application layout file, there are conflicts which make the dashboard view weird. If I move the application layout stylesheet tag to a _header.html.erb partial which is rendered with every view in the non-dashboard section like below, it doesn't work. How must I call them?

<%= stylesheet_link_tag "application" %>
<header>
     <div id="headercontainer">
..
like image 530
Simpleton Avatar asked Dec 11 '11 16:12

Simpleton


1 Answers

you should use a yield statement in your application.html.erb in the head element as such:

<head>
  <%= yield :head %>
</head>

then in your view, you would use a content_for tag:

<% content_for :head do %>
  <%= stylesheet_link_tag "dashboard" %>
<% end %>

also read the rails docs on nested layouts. it'll teach you how to get fancy with this paradigm

like image 130
Chris Drappier Avatar answered Nov 15 '22 04:11

Chris Drappier