james
james

Reputation: 4049

Rails different headers for different pages with if statement, but headers persisting

I wrote a simple if statement to change the headers depending on which page the user is on like so:

<% if @miscpage = true %>
  <%= render 'layouts/mischeader' %>
<% elsif @gallerypage = true %> 
  <%= render 'layouts/galleryhead' %>
<% elsif @photopage = true %>
  <%= render 'layouts/photoheader' %>
<% end %> 

Then in the controller for the specific pages the @variable is set to true. The problem though is that the mischeader persists whenever I go to any other page. It's like the application layout is not resetting the rendering process.

I've tested this, by switching the order of the if statements above, and indeed if @photopage=true were evaluated first, it's the photoheader that would persist across all pages.

I've further tested by writing separate if statements like below, and indeed, now I get two headers after visiting both the miscpage and the photopage.

<% if @miscpage = true %>
  <%= render 'layouts/mischeader' %>
<% end %>

<% if @gallerypage = true %> 
  <%= render 'layouts/galleryhead' %>
<% end %>

<% if @photopage = true %>
  <%= render 'layouts/photoheader' %>
<% end %> 

Any idea how to fix this problem?

Upvotes: 0

Views: 300

Answers (2)

vee
vee

Reputation: 38645

You really don't need to compare the variables against true boolean in an if statement. But for your case you are using assignment operator =, either change them to comparison operator == or get rid of the = true from all if and elsif statements.

Upvotes: 1

Philip Hallstrom
Philip Hallstrom

Reputation: 19879

x = true is an assignment. You are assigning true to x. It will always succeed. You want x == true to test equality.

= vs ==

Upvotes: 0

Related Questions