Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails, render a different header just on homepage

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?

like image 729
Roberto Pezzali Avatar asked Mar 25 '14 20:03

Roberto Pezzali


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() .

How can you tell Rails to render without a layout?

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.

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.


2 Answers

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 %>
like image 59
Bryan Clark Avatar answered Oct 26 '22 06:10

Bryan Clark


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 %>
like image 31
Kirti Thorat Avatar answered Oct 26 '22 06:10

Kirti Thorat