开发者

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'开发者_如何学Gove 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜