I'm creating a rails app and I must differentiate the header for home page.
I already created a partial with the _home_header version and the _header version to use in every page, but I don't know how can I manage the change.
The header is included in my layout, and I render the same layout for every page. How can I tell to "layout" to use the _home_header version instead of the standard version when I request homepage?
You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .
2.2. 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.
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.
I would use the current_page?
helper and look at the root_path
.
# app/views/layouts/application.html.erb
<% if current_page?(root_path) %>
<%= render 'layouts/home_header' %>
<% else %>
<%= render 'layouts/header' %>
<% end %>
Use something like this in application.html.erb
<% if request.original_url == root_url %> ## Specify the home url instead of root_url(if they are different)
<%= render 'layouts/home_header' %> ## Assuming that _home_header.html.erb is under layouts directory
<% else %>
<%= render 'layouts/header' %> ## Assuming that _header.html.erb is under layouts directory
<% end %>
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