Fri 16 Mar 2007
Optional Sidebars
Posted by nola under Ruby On Rails
Comments Off
For one of my projects, my layout includes optional sidebars on either side. I want to design this without tables.. so at first I thought something like this:
-
<div id="leftside">
-
<%= render :partial => "left_#{@controller.action_name}" %>
-
</div>
-
<div id="rightside">
-
<%= render :partial => "right_#{@controller.action_name}" %>
-
</div>
SO for index page, it will render the partials _left_index.rhtml and _right_index.rhtml ... but say if you don't want a side bar, then it can't find one... and you get an error. I would like to not have to resort to setting variables @left_sidebar =true @right_sidebar = false and have to do something like this:
-
<% if @rightsidbar_on %>
-
<div id="rightside">
-
<%= render :partial => "right_#{@controller.action_name}" %>
-
</div>
-
<% end %>
Ideally I'd like a render_if_exist but I don't see anything like that in rails. Ideas??
Update
Almost as soon as I finished the post and sent to my friend Peter.. I decide to google for "render_partial_if_exists" and found this blog posting by Jeremy Hubert -- the exact solution! Peter was also in the process of adding a comment at the same time telling me with nearly the same solution. -- Check out Jeremys post, its awesome.
