passing optional parameters to partials

May 13, 2009 at 4:38 pm (Partials) (, , , )

Today i got an instance where i want to use a parameter in the partial, which may or may not be passed.This partial is called from two places, one which passes while other does not pass a specific parameter. The partial just displays the parameter value.

<%= passed_parameter %>

If an instance variable is used, the <%= … %> will result in an empty string if the instance variable (@abcdef) id not defined. But if its a local variable like the one passed to the partials using LOCALS, it will throw an error.

In these cases, the best thing to do is, add this at the first line of the PARTIAL.

<% passed_parameter ||= DEFAULT_VALUE %>

This has to be done for whatever optional parameters might be used.


Permalink Leave a Comment