<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PMA Media Group&#187; HABTM</title>
	<atom:link href="http://www.pmamediagroup.com/tag/habtm/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pmamediagroup.com</link>
	<description>Unique Marketing Techniques and Strategies with Guaranteed Results!</description>
	<lastBuildDate>Fri, 27 Aug 2010 18:14:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Has and Belongs To Many (HABTM) Checkboxes With Rails</title>
		<link>http://www.pmamediagroup.com/2009/04/has-and-belongs-to-many-habtm-checkboxes-with-rails/</link>
		<comments>http://www.pmamediagroup.com/2009/04/has-and-belongs-to-many-habtm-checkboxes-with-rails/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 21:50:35 +0000</pubDate>
		<dc:creator>Aaron Murphy</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[checkbox]]></category>
		<category><![CDATA[HABTM]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[railscast]]></category>

		<guid isPermaLink="false">http://www2.pmamediagroup.com/?p=10</guid>
		<description><![CDATA[I was trying to setup an HABTM list with a checkbox, similar to Ryan Bates&#8217; railscast. The problem was the empty http posts when all checkboxes are cleared.

The code that I was emulating is:

# products_controller.rb
def update
  params&#91;:product&#93;&#91;:category_ids&#93; &#124;&#124;= &#91;&#93;
  #...
end

The problem I was having is that

params&#91;:product&#93;

was nil as the http post sent nothing [...]]]></description>
			<content:encoded><![CDATA[<p>I was trying to setup an <a href="http://en.wikipedia.org/wiki/HABTM">HABTM</a> list with a checkbox, similar to Ryan Bates&#8217; <a href="http://railscasts.com/episodes/17-habtm-checkboxes">railscast</a>. The problem was the empty <a href="http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods">http posts</a> when all checkboxes are cleared.</p>
<p><span id="more-10"></span></p>
<p>The code that I was emulating is:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># products_controller.rb</span>
<span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#5A0A0A; font-weight:bold;">update</span>
  params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:product</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:category_ids</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#008000; font-style:italic;">#...</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The problem I was having is that</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:product</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>was nil as the http post sent nothing because I only had checkboxes on my form. Defaulting to</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>wasn&#8217;t valid for the</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">update_attributes</pre></div></div>

<p>method.</p>
<p>The hack that I finally came up with was like this:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># products_controller.rb</span>
<span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#5A0A0A; font-weight:bold;">update</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:product</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#0000FF; font-weight:bold;">nil</span>?
    <span style="color:#0066ff; font-weight:bold;">@products</span>.<span style="color:#9900CC;">categories</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">else</span>
    <span style="color:#0066ff; font-weight:bold;">@products</span>.<span style="color:#9900CC;">update_attributes</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:product</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#5A0A0A; font-weight:bold;">redirect_to</span> <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:index</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>So it turns out, in this HABTM scenario, one can address the entire list of categories with an array.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pmamediagroup.com/2009/04/has-and-belongs-to-many-habtm-checkboxes-with-rails/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: www.pmamediagroup.com @ 2012-02-05 02:49:21 -->
