1

I have one HTML file located in app/views/layouts/application.html.erb

I use <%=yield=> to render the HTML code from other models. The problem is that I don't know how to distinguish the CSS code. I have a universal CSS code, but each html file needs a different one.

here is my CSS line in app/views/layouts/application.html.erb

<%= stylesheet_link_tag "scaffold" %>
<%= stylesheet_link_tag "welcome_screen/my_CSS.css", :media => "all" %>

2 Answers 2

2

put a named yield in the head of the layout file, then use content_for in your specific view

# application.html.erb
<head>
    <%= stylesheet_link_tag "scaffold" %>
    <%= yield(:head) %>
    # ...
</head>
<body>
    <%= yield %>
    # ...
</body>

then your view code

# welcome_screen.html.erb
<% content_for(:head) do %>
    <%= stylesheet_link_tag "welcome_screen/my_CSS.css", :media => "all" %>  
<% end %>
Sign up to request clarification or add additional context in comments.

Comments

0

You can add <%= yield :head %> in the <head> tag in application.html.erb. Later in you views if you need some css file

<% content_for :head do %>
  <%= stylesheet_link_tag "some_uniq.css" %>
<% end %>

You can read this for better understaning of how yield command works.

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.