TheExit
TheExit

Reputation: 5320

Rails - Passing an option variable in a partial

I'm building a header partial for a view. I call the partial as following:

<%= render :partial =>"project/header", :locals => {:right_header => 'BLAH BLAH'} %>

The header has a default right_header, but I'd like the option to overwrite it:

<div id="header">
<span class="right">
 Standard Header here
</span>
</div>

The deal is when calling the partial, right_header won't always be defined, I'd like for it to be optional, but that's where I'm struggling and rails keeps erroring... In the partial I've been trying:

<% if right_header.empty? %>
 default header....
<% else %>
 <%= right_header %>
<% end %>

Suggestions? Am I passing this correctly to the partial with locals?

Thank you

Upvotes: 3

Views: 1535

Answers (1)

nonopolarity
nonopolarity

Reputation: 150976

use

if defined? right_header

another way is

right_header ||= 'default'

in the view. so if right_header is not passed in, its value will be default. You can pass in any value too, and later on just do things according to the value of right_header.

Upvotes: 6

Related Questions