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
?
You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .
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.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With