Sunday, February 8, 2009

Blocks in View

I had a bunch of "if logged_in? && current_user.admin?" in my views, which made it very ugly. I saw an old video (it is also covered again) from Railscasts and knew this would be my answer.

However, I wanted more customization though. What if I didn't want to wrap my admin_area in a div? What if I wanted to add conditions on the fly and not make a zillion [enter_some_name]_area methods?

This would be particularly useful if, for example, I only want the "delete" option of an object to appear to admins and if the object if destroyable.

In haml:

- admin_area(:conditions => @object.destroyable?) do
= link_to "Destroy", ...


Take a look at my helper method to see what I did. I'd appreciate any comments!

1 comment:

mark said...

Thanks! Exactly what I was looking for! :)