Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby on rails - CSS for a partial

I'm developing a website using Ruby on Rails. Right now, I've added a bootstrap navigation bar in a partial file, "_navheader.html.erb" in my layouts folder. I render the partial right after the body tag in application.html.erb. The problem is that I have some CSS code for the nav bar in a separate css file. How do I link the CSS file, navheader.css.scss to the html file, _navheader.html.erb?

like image 261
user2771150 Avatar asked Nov 24 '15 09:11

user2771150


People also ask

How do you render a partial?

You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .

What is Local_assigns?

local_assigns is a Rails view helper method that you can check whether this partial has been provided with local variables or not. Here you render a partial with some values, the headline and person will become accessible with predefined value.

How can you tell Rails to render without a layout?

By default, if you use the :plain option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option and use the . text. erb extension for the layout file.


1 Answers

In your application.html.erb's head section, you can write

<% if content_for?(:head) %>
    <%= yield(:head) %>
<% end %> 

And in your _navheader.html.erb, you define your css file (with respective path) like this:

<% content_for :head do %>
     <link rel="stylesheet" href="/assets/navheader.css.scss"> 
<% end %>

Hope it helps!

like image 131
Dusht Avatar answered Sep 30 '22 14:09

Dusht