<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: How-To Make a Rails Partial With Optional Locals (Parameters)</title>
	<atom:link href="http://hackd.wordpress.com/2007/08/25/how-to-make-a-rails-partial-with-optional-locals-parameters/feed/" rel="self" type="application/rss+xml" />
	<link>http://hackd.wordpress.com/2007/08/25/how-to-make-a-rails-partial-with-optional-locals-parameters/</link>
	<description>A collection of cool ideas we come across while building ThriveSmart.</description>
	<lastBuildDate>Wed, 30 Sep 2009 04:19:14 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: scottwb</title>
		<link>http://hackd.wordpress.com/2007/08/25/how-to-make-a-rails-partial-with-optional-locals-parameters/#comment-329</link>
		<dc:creator>scottwb</dc:creator>
		<pubDate>Tue, 18 Nov 2008 00:26:26 +0000</pubDate>
		<guid isPermaLink="false">http://hackd.wordpress.com/2007/08/25/how-to-make-a-rails-partial-with-optional-locals-parameters/#comment-329</guid>
		<description>Nice writeup - I will have to try this out. Oddly, I have never had a problem with the defined? way that you started out with. I totally agreed with treating partials like methods - from day one on Rails I have been putting this kind of thing at the top of each partial:

name = nil unless defined?(name)
phone = nil unless defined?(phone)

In fact, I often use this to define default value for the locals that are passed in, while letting an explicitly passed-in value of nil mean that the default value should be used, such as:

name = &#039;unknown&#039; unless (defined?(name) &amp;&amp; name)

That was all using RHTML...recently I started converting an existing rhtml-based project to haml and ran into a specific problem with this technique...but only when using a boolean value for a local. Consider that I want passing the local specifically to true or false to be preserved, but if you don&#039;t specify it or pass in nil, then it should use a default value of true. So...I did this:

show_name = true unless (defined?(show_name) &amp;&amp; !show_name.nil?)

This works like a charm when the partial is rhtml, but with haml, it seems that if I explicitly pass false for show_name to the partial, that it gets converted to nil. Doh!

Not sure what path I am going to choose to address this in a large existing code base that relies on it...I might have to try out the _haml_local_assigns tactic mentioned above.

For a simple code example that reproduces this problem, see my writeup on it at: http://sleeplesscoding.blogspot.com/2008/11/bug-in-haml-partial-rendering.html</description>
		<content:encoded><![CDATA[<p>Nice writeup &#8211; I will have to try this out. Oddly, I have never had a problem with the defined? way that you started out with. I totally agreed with treating partials like methods &#8211; from day one on Rails I have been putting this kind of thing at the top of each partial:</p>
<p>name = nil unless defined?(name)<br />
phone = nil unless defined?(phone)</p>
<p>In fact, I often use this to define default value for the locals that are passed in, while letting an explicitly passed-in value of nil mean that the default value should be used, such as:</p>
<p>name = &#8216;unknown&#8217; unless (defined?(name) &amp;&amp; name)</p>
<p>That was all using RHTML&#8230;recently I started converting an existing rhtml-based project to haml and ran into a specific problem with this technique&#8230;but only when using a boolean value for a local. Consider that I want passing the local specifically to true or false to be preserved, but if you don&#8217;t specify it or pass in nil, then it should use a default value of true. So&#8230;I did this:</p>
<p>show_name = true unless (defined?(show_name) &amp;&amp; !show_name.nil?)</p>
<p>This works like a charm when the partial is rhtml, but with haml, it seems that if I explicitly pass false for show_name to the partial, that it gets converted to nil. Doh!</p>
<p>Not sure what path I am going to choose to address this in a large existing code base that relies on it&#8230;I might have to try out the _haml_local_assigns tactic mentioned above.</p>
<p>For a simple code example that reproduces this problem, see my writeup on it at: <a href="http://sleeplesscoding.blogspot.com/2008/11/bug-in-haml-partial-rendering.html" rel="nofollow">http://sleeplesscoding.blogspot.com/2008/11/bug-in-haml-partial-rendering.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Passing Parameters to Partials &#124; Question Defense</title>
		<link>http://hackd.wordpress.com/2007/08/25/how-to-make-a-rails-partial-with-optional-locals-parameters/#comment-327</link>
		<dc:creator>Passing Parameters to Partials &#124; Question Defense</dc:creator>
		<pubDate>Mon, 10 Nov 2008 08:40:08 +0000</pubDate>
		<guid isPermaLink="false">http://hackd.wordpress.com/2007/08/25/how-to-make-a-rails-partial-with-optional-locals-parameters/#comment-327</guid>
		<description>[...] Making a partial use optional parameters - Basically, set the parameter to nil inside the partial (e.g. title = nil unless defined?(title)) [...]</description>
		<content:encoded><![CDATA[<p>[...] Making a partial use optional parameters &#8211; Basically, set the parameter to nil inside the partial (e.g. title = nil unless defined?(title)) [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Trupiano</title>
		<link>http://hackd.wordpress.com/2007/08/25/how-to-make-a-rails-partial-with-optional-locals-parameters/#comment-322</link>
		<dc:creator>John Trupiano</dc:creator>
		<pubDate>Fri, 05 Sep 2008 17:04:43 +0000</pubDate>
		<guid isPermaLink="false">http://hackd.wordpress.com/2007/08/25/how-to-make-a-rails-partial-with-optional-locals-parameters/#comment-322</guid>
		<description>Just wanted to say thanks.  I google for this every time I need to remember the syntax/variable name!</description>
		<content:encoded><![CDATA[<p>Just wanted to say thanks.  I google for this every time I need to remember the syntax/variable name!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Red M@</title>
		<link>http://hackd.wordpress.com/2007/08/25/how-to-make-a-rails-partial-with-optional-locals-parameters/#comment-299</link>
		<dc:creator>Red M@</dc:creator>
		<pubDate>Mon, 10 Mar 2008 20:30:16 +0000</pubDate>
		<guid isPermaLink="false">http://hackd.wordpress.com/2007/08/25/how-to-make-a-rails-partial-with-optional-locals-parameters/#comment-299</guid>
		<description>Thanks.  That was very helpful!

I totally agree that partials are analogous to methods.  I am starting to learn to refactor reused peices of views into partials.</description>
		<content:encoded><![CDATA[<p>Thanks.  That was very helpful!</p>
<p>I totally agree that partials are analogous to methods.  I am starting to learn to refactor reused peices of views into partials.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Marklund</title>
		<link>http://hackd.wordpress.com/2007/08/25/how-to-make-a-rails-partial-with-optional-locals-parameters/#comment-298</link>
		<dc:creator>Peter Marklund</dc:creator>
		<pubDate>Fri, 07 Mar 2008 13:47:00 +0000</pubDate>
		<guid isPermaLink="false">http://hackd.wordpress.com/2007/08/25/how-to-make-a-rails-partial-with-optional-locals-parameters/#comment-298</guid>
		<description>An alternative approach to querying the local_assigns hash for each optional local would be to use a local hash called options, analogous to many of the methods in the Rails API. Then at the top of your partial just do:

options &#124;&#124;= {}

This approach of course makes setting the optional locals a little clumsier when invoking the partial. I like though that it makes it more clear when you are referencing an optional param in your partial. For clarity and to keep them separate from local variables, I would have prefered if Rails would have set all params passed to a partial in a hash called say partial.</description>
		<content:encoded><![CDATA[<p>An alternative approach to querying the local_assigns hash for each optional local would be to use a local hash called options, analogous to many of the methods in the Rails API. Then at the top of your partial just do:</p>
<p>options ||= {}</p>
<p>This approach of course makes setting the optional locals a little clumsier when invoking the partial. I like though that it makes it more clear when you are referencing an optional param in your partial. For clarity and to keep them separate from local variables, I would have prefered if Rails would have set all params passed to a partial in a hash called say partial.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: roger pack</title>
		<link>http://hackd.wordpress.com/2007/08/25/how-to-make-a-rails-partial-with-optional-locals-parameters/#comment-288</link>
		<dc:creator>roger pack</dc:creator>
		<pubDate>Thu, 17 Jan 2008 21:27:19 +0000</pubDate>
		<guid isPermaLink="false">http://hackd.wordpress.com/2007/08/25/how-to-make-a-rails-partial-with-optional-locals-parameters/#comment-288</guid>
		<description>works like a charm. Thanks!</description>
		<content:encoded><![CDATA[<p>works like a charm. Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roman Gonzalez</title>
		<link>http://hackd.wordpress.com/2007/08/25/how-to-make-a-rails-partial-with-optional-locals-parameters/#comment-211</link>
		<dc:creator>Roman Gonzalez</dc:creator>
		<pubDate>Wed, 19 Dec 2007 21:09:53 +0000</pubDate>
		<guid isPermaLink="false">http://hackd.wordpress.com/2007/08/25/how-to-make-a-rails-partial-with-optional-locals-parameters/#comment-211</guid>
		<description>If you want this to work with HAML you should use _haml_local_assigns instead of local_assigns.</description>
		<content:encoded><![CDATA[<p>If you want this to work with HAML you should use _haml_local_assigns instead of local_assigns.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arron</title>
		<link>http://hackd.wordpress.com/2007/08/25/how-to-make-a-rails-partial-with-optional-locals-parameters/#comment-95</link>
		<dc:creator>Arron</dc:creator>
		<pubDate>Fri, 02 Nov 2007 06:25:00 +0000</pubDate>
		<guid isPermaLink="false">http://hackd.wordpress.com/2007/08/25/how-to-make-a-rails-partial-with-optional-locals-parameters/#comment-95</guid>
		<description>I was having this problem too...

It&#039;s strange that there&#039;s not an easy DRY solution for this in rails. I originally used your implementation, but then decided it was way too much writing for my taste (my fingers get tired/sore easily).

I hacked up my own little plugin that does something similar (here: http://geekninja.blogspot.com/2007/09/rails-plugin-optional-parameters-for.html  ).

Simple, but works alright in my opinion.</description>
		<content:encoded><![CDATA[<p>I was having this problem too&#8230;</p>
<p>It&#8217;s strange that there&#8217;s not an easy DRY solution for this in rails. I originally used your implementation, but then decided it was way too much writing for my taste (my fingers get tired/sore easily).</p>
<p>I hacked up my own little plugin that does something similar (here: <a href="http://geekninja.blogspot.com/2007/09/rails-plugin-optional-parameters-for.html" rel="nofollow">http://geekninja.blogspot.com/2007/09/rails-plugin-optional-parameters-for.html</a>  ).</p>
<p>Simple, but works alright in my opinion.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: gmarik</title>
		<link>http://hackd.wordpress.com/2007/08/25/how-to-make-a-rails-partial-with-optional-locals-parameters/#comment-81</link>
		<dc:creator>gmarik</dc:creator>
		<pubDate>Sat, 27 Oct 2007 11:33:46 +0000</pubDate>
		<guid isPermaLink="false">http://hackd.wordpress.com/2007/08/25/how-to-make-a-rails-partial-with-optional-locals-parameters/#comment-81</guid>
		<description>2Matt:
i think it&#039;s just enough

title = local_assigns[:title]
Isn&#039;t it?

Or just use local_assigns[:title] instead title - as it&#039; wont raise any errors</description>
		<content:encoded><![CDATA[<p>2Matt:<br />
i think it&#8217;s just enough</p>
<p>title = local_assigns[:title]<br />
Isn&#8217;t it?</p>
<p>Or just use local_assigns[:title] instead title &#8211; as it&#8217; wont raise any errors</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt</title>
		<link>http://hackd.wordpress.com/2007/08/25/how-to-make-a-rails-partial-with-optional-locals-parameters/#comment-44</link>
		<dc:creator>Matt</dc:creator>
		<pubDate>Tue, 16 Oct 2007 18:47:52 +0000</pubDate>
		<guid isPermaLink="false">http://hackd.wordpress.com/2007/08/25/how-to-make-a-rails-partial-with-optional-locals-parameters/#comment-44</guid>
		<description>Hey... after much help in the comments, I&#039;m switching to another method that seems to be used in core rails: param = default_value unless !local_assigns[:param].nil? -- although it won&#039;t work with HAML</description>
		<content:encoded><![CDATA[<p>Hey&#8230; after much help in the comments, I&#8217;m switching to another method that seems to be used in core rails: param = default_value unless !local_assigns[:param].nil? &#8212; although it won&#8217;t work with HAML</p>
]]></content:encoded>
	</item>
</channel>
</rss>
