SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    May 2001
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    check_box selection

    I'm using the check_box form helper and trying to do the following:
    I have an array @collections I want to provide checkboxes for.
    And I also have an @articles object that contains a "collections" method.
    I want to have collections that an article belongs to selected.

    I've been trying different variations of this to no avail:
    from my controller
    PHP Code:
       def new
         @
    article Article.new
         @
    collections Collection.find(:all)
       
    end 
    snipped from my form
    PHP Code:
     <% for collection in @collections %>
       <
    tr><td align="right"><%= check_box 'collection''id' %></td><td><%= collection.name %></td></tr>
     <% 
    end %> 

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <% for @collection in @collections %>
       <tr><td align="right"><%= check_box 'collection', 'id' %></td><td><%= @collection.name %></td></tr>
    <% end %>
    Note the @collection.

  3. #3
    SitePoint Zealot
    Join Date
    May 2001
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Fenrir2,
    Thanks. That checks all of the items of @collections.
    What I'm trying to do is only check or select only the items of @collections that happen to be in @article.collections
    so If @collections contains ["a", "b", "c", "d"] and @article.collections contains ["a", "b"] I only want a and b checked

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then this won't work ;-).

    Maybe:

    Code:
    <% for collection in @collections %>
       <%= check_box_tag "collections", collection.id, @article.collections.include?(collection) %> <%= @collection.name %>
    <% end %>

  5. #5
    SitePoint Zealot
    Join Date
    May 2001
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fenrir2, that did work. Thanks.

    But I'm confused as to why it works, what's the difference between check_box_tag and check_box?

  6. #6
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    check_box is designed to work with your active record model attributes where as check_box_tag is a plain jane tag generating helper.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •