PaulT12
PaulT12

Reputation: 71

Rails: How to load a specific stylesheet for a specific layout?

I'm new to Rails and am having trouble getting a layout (which is being used as a unique, static, landing page) to load a specific stylesheet I want to associate with it. E.g.: www.landing.com to load landing.scss.erb.

As background, all other routes are going to different pages with different and much more complicated layouts which I have stored in assets/stylesheets. E.g.: www.landing.com/A (a.scss.erb); www.landing.com/B (b.scss.erb). The landing page stylesheet is also in assets/stylesheets as landing.scss.erb.

In my home controller I have an action which renders the landing page:

def landing
        render :layout => "landing"
    end

And in my routes.rb: get 'landing' => 'home#landing'

In my views/layouts I have a file landing.html.erb in which I am using <%= stylesheet_link_tag 'landing.scss.erb', media: 'all' %> in my head to try and load this specific stylesheet, however, the landing page is not loading.

If, in my application.scss helper file, I include @import "landing", then all of my other pages being dictated by application.html.erb will get the landing.scss.erb styling which is not what I want.

Any help greatly appreciated!

Upvotes: 3

Views: 3395

Answers (1)

PaulT12
PaulT12

Reputation: 71

I figured out what was wrong. I believe my syntax for calling the stylesheet in my layout was incorrect.

When I changed it from <%= stylesheet_link_tag '/assets/stylesheets/landing.scss.erb', media: 'all' %>', media: 'all' %>

to

<%= stylesheet_link_tag 'landing', media: 'all' %>, I got a new error telling me to add

Rails.application.config.assets.precompile += %w( landing.css ) to config/initializers/assets.rb.

I added it, restarted my server and my new layout was correctly rendering the specific stylesheet.

However, I still need to figure out exactly what this precompling code is doing and why it was needed. Any guidance/clarification is appreciated.

Upvotes: 2

Related Questions