tag:www.rhnh.net,2008:/routing
Routing - Xavier Shay's Blog
2009-01-26T04:01:02Z
Enki
Xavier Shay
notreal@rhnh.net
tag:www.rhnh.net,2008:Post/793
2009-01-26T04:01:00Z
2009-01-26T04:01:02Z
Singleton resource, pluralized controller fix for rails
<p><code>map.resource</code> still looks for a pluralized controller. This has always bugged me. Here’s a quick monkey patch to fix. Tested on rails 2.2.2.</p><table class="CodeRay"><tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt>5<tt>
</tt>6<tt>
</tt>7<tt>
</tt>8<tt>
</tt>9<tt>
</tt><strong>10</strong><tt>
</tt>11<tt>
</tt>12<tt>
</tt>13<tt>
</tt></pre></td>
<td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="c"># config/initializers/singleton_resource_fix.rb</span><tt>
</tt><span class="r">module</span> <span class="cl">ActionController</span><tt>
</tt> <span class="r">module</span> <span class="cl">Resources</span><tt>
</tt> <span class="r">class</span> <span class="cl">SingletonResource</span> < <span class="co">Resource</span> <span class="c">#:nodoc:</span><tt>
</tt> <span class="r">def</span> <span class="fu">initialize</span>(entity, options)<tt>
</tt> <span class="iv">@singular</span> = <span class="iv">@plural</span> = entity<tt>
</tt> <span class="c"># options[:controller] ||= @singular.to_s.pluralize</span><tt>
</tt> options[<span class="sy">:controller</span>] ||= <span class="iv">@singular</span>.to_s <span class="c"># This is the only line to change</span><tt>
</tt> <span class="r">super</span><tt>
</tt> <span class="r">end</span><tt>
</tt> <span class="r">end</span><tt>
</tt> <span class="r">end</span><tt>
</tt><span class="r">end</span><tt>
</tt></pre></td>
</tr></table>
<table class="CodeRay"><tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt>5<tt>
</tt>6<tt>
</tt></pre></td>
<td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="c"># config/routes.rb</span><tt>
</tt><span class="c"># before fix</span><tt>
</tt>map.resource <span class="sy">:session</span>, <span class="sy">:controller</span> => <span class="s"><span class="dl">'</span><span class="k">sessions</span><span class="dl">'</span></span><tt>
</tt><tt>
</tt><span class="c"># after fix</span><tt>
</tt>map.resource <span class="sy">:session</span><tt>
</tt></pre></td>
</tr></table>