简体   繁体   English

Rails:如何为特定布局加载特定样式表?

[英]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. 我是Rails的新手,在获取布局(用作唯一,静态,着陆页)时无法加载要与之关联的特定样式表。 Eg: www.landing.com to load landing.scss.erb . 例如:www.landing.com加载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 . 作为背景,所有其他路线都将转到具有不同且更为复杂的布局的不同页面,这些布局我已存储在assets/stylesheets Eg: www.landing.com/A ( a.scss.erb) ; 例如:www.landing.com/A( a.scss.erb) www.landing.com/B (b.scss.erb). www.landing.com/B(b.scss.erb)。 The landing page stylesheet is also in assets/stylesheets as landing.scss.erb . 着陆页样式表也位于assets/stylesheetslanding.scss.erb

In my home controller I have an action which renders the landing page: 在我的home controller我有一个渲染目标页面的动作:

def landing
        render :layout => "landing"
    end

And in my routes.rb : get 'landing' => 'home#landing' 然后在我的routes.rbget '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. 在我的views/layouts我有一个文件landing.html.erb中我使用<%= stylesheet_link_tag 'landing.scss.erb', media: 'all' %>在我的head ,试图加载这个特定的样式表,但,着陆页未加载。

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. 如果在我的application.scss帮助文件中包含@import "landing" ,那么由application.html.erb指示的所有其他页面将获得不是我想要的landing.scss.erb样式。

Any help greatly appreciated! 任何帮助,不胜感激!

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' %> 当我从<%= stylesheet_link_tag '/assets/stylesheets/landing.scss.erb', media: 'all' %>', media: 'all' %>更改时<%= 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 <%= stylesheet_link_tag 'landing', media: 'all' %> ,我收到一个新错误,告诉我要添加

Rails.application.config.assets.precompile += %w( landing.css ) to config/initializers/assets.rb . Rails.application.config.assets.precompile += %w( landing.css )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. 任何指导/澄清表示赞赏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM