<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>The Vose Way: Tag check_box_tag</title>
    <link>http://www.chuckvose.com/articles/tag/check_box_tag</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>A Blog about Butter, Cheese, and all things Vosian</description>
    <item>
      <title>Pagination, Sets, Checkboxes</title>
      <description>&lt;p&gt;When I was at RailsConf2008 I learned a lot about life and Rails but there were some nagging problems that lingered after me for a long time.&lt;/p&gt;


	&lt;p&gt;At one point in the talks there was a discussion of how to make listing pages have checkboxes and how to handle this data. The problem is complicated by pagination and filtering so it&amp;#8217;s actually a really good problem to talk about.&lt;/p&gt;


	&lt;p&gt;The proposed solution was to send back a structure that looked something like this:&lt;/p&gt;


&lt;pre&gt;
[{:id =&amp;gt; 1, :activated =&amp;gt; "+", :contact =&amp;gt; "-"}, ...]
&lt;/pre&gt;

	&lt;p&gt;At this point I was probably playing pogs in the audience with Ryan Schenk so the details are lost to time but the concept remains.&lt;/p&gt;


	&lt;p&gt;It seems needlessly complex though. A very interesting idea, no doubt about it, and it certainly has its merit but it seems very complex to me when we&amp;#8217;re talking about booleans. Also, I feel like it&amp;#8217;s worth mentioning that this method is much faster than the below method but somehow that doesn&amp;#8217;t turn me off that badly.&lt;/p&gt;


	&lt;p&gt;Instead of the above I took the already well accepted practice of using the check_box_tag in the view with the addition of a hidden field which would tell me which objects to care about:&lt;/p&gt;


&lt;pre&gt;
&amp;lt;% @users.each do |user| %&amp;gt;
  &amp;lt;%= hidden_field_tag('seen[]', user.id) -%&amp;gt;
  &amp;lt;%= check_box_tag 'activated[]', user.id -%&amp;gt;
&amp;lt;% end %&amp;gt;
&lt;/pre&gt;

	&lt;p&gt;In the controller it&amp;#8217;s as easy as loading up params[:seen] and loading each object:&lt;/p&gt;


&lt;pre&gt;
def index
  if request.post?
    activated_ids = params[:activated].collect {|id| id.to_i} if params[:activated]
    seen_ids = params[:seen].collect {|id| id.to_i} if params[:seen]

    if activated_ids
      seen_ids.each do |id|
        r = User.find_by_id(id)
        r.activated = activated_ids.include?(id)
        r.save
      end
    end
  end
end
&lt;/pre&gt;

	&lt;p&gt;So what I don&amp;#8217;t get is whether this is wrong on some moral stance or if I really didn&amp;#8217;t understand what the presenter was getting at. Yes, he loads and saves fewer objects, which could be important. But he also introduced what seems to me to be a lot of complexity into a fairly simple process.&lt;/p&gt;


	&lt;p&gt;At any rate, this code is here in case someone needs to find it until I find what the presenter was proposing. I seem to recall being impressed at the time so there must be something I&amp;#8217;ve forgotten.&lt;/p&gt;</description>
      <pubDate>Mon, 25 Aug 2008 15:29:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:1f1a423f-d1ae-4d35-a981-25ca8ca91372</guid>
      <author>vosechu@create-on.com (Chuck Vose)</author>
      <link>http://www.chuckvose.com/articles/2008/08/25/pagination-sets-checkboxes</link>
      <category>Ruby</category>
      <category>Rails</category>
      <category>Programming</category>
      <category>check_box</category>
      <category>check_box_tag</category>
      <category>Rails</category>
      <category>Ruby</category>
      <category>view</category>
      <category>list</category>
      <category>index</category>
      <category>pagination</category>
      <category>filter</category>
      <category>check</category>
      <category>boxes</category>
      <category>multiple</category>
    </item>
  </channel>
</rss>

