Optional Atrributes for Partials

I just stumbled about a nice and only a little dirty approach to make partial attributes optional.

Helper

#app/helpers/application_helper.rb
def options;{};end
def item;nil;end

View

render :partial=>'xyz', :item=>@user, :options=>{:show_something=>true}
OR
render :partial=>'xyz'

Inside partial

...
text = options[:show_something] if item
...

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s