Forum: Ruby on Rails generated check_box(es) & committing to db

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mike D. (Guest)
on 2007-01-24 21:13
hello:

I have some view code generating a list of check boxes based on the
values in a table:

<% @categories = Category.find(:all, :order => "name" ) %>
<% for category in @categories -%>
  <%= check_box ("cats", "category_id", options = {}, category.id, 0 )
%>&nbsp;&nbsp;<%= category.name %><br>
<% end %>

"cats" represents a many_to_many relationship between the entity
"Category" and another entity.  I'm creating "cats" in my controller in
a new method:

The Category table contains:

id
goal_id(other entity)
category_id(the thing I'm capturing with the check boxes)
And a timestamp

In my controller, all i'm doing is:

  @cats = CategoryGoal.new
  @cats.goal_id = @goal.id

So what I want to happen is when the checkbox is click in the view, and
the user clicks "create" in the form, I want the value of "category.id"
inserted into the table "Category" with goal_id.  I already have
goal.id.

Where am I getting stuck?  Thanks again for your help!
Mike D. (Guest)
on 2007-01-24 21:36
Hi:

I've now updated my view code:

<% @categories = Category.find(:all, :order => "name" ) %>
<% fields_for :category_goals do |f| %>
  <% for category in @categories -%>
    <%= f.check_box :entity_id %>&nbsp;&nbsp;<%= category.name %><br>
  <% end %>
<% end %>

My checkboxes generate fine, but I still have no way of storing the
category.id as the entity_id even though the fields_for allows me to
have these categories within this goal.  The problem is that I don't
know how to make the bridge between the multiple responses I would get
when the check_box is checked (in f.check_box) and the one action of
submitting this form.

Thanks again!

Mike
This topic is locked and can not be replied to.