Search code examples
ruby-on-railscssstylesheet

How do you associate a css/sass stylesheet to a view in rails?


This is a really generic question, to which I haven't found a simple answer.

I'm dealing with messy legacy code that specifies style several times for the same classes/views. I have .sass files in app/styles, .css files in public/stylesheets and public/css

I don't understand which stylesheets includes which, or if they ever do. How do you match stylesheets to a specific view in rails? How do you define hierarchy between style so ones can override others?

What's the rails default for matching styles to views? I don't see any stylesheet_link_tag used in the app


Solution

  • The best way would be to put a named yield in your application layout:

    <%= yield :head %>
    

    Then use a content for block in your view:

    <% content_for :head do %>
       <%= stylesheet_link_tag :my_css -%>
    <% end %>