All,
I’m populating a view with checkboxes in a list. I want to be able to
assign to my has_many relationship using the post values of these
checkboxes. Essentially, I want to populate the relationship table
which will create the correct join values in my model.
Here is my check_box_tag code:
<%= check_box_tag("@current_job[target_list_ids][]",
tlist.DataSetID,
@current_job.target_lists.include?(tlist)) %>
So, in this example, tlist is an element of another array that I’m
starting with to give me the list of choices. But what I want is to
assign these choices into the “target_lists” collection of my
@current_job object (again, via the “through” table based on my has_many
definition.
current_job :has_many target_lists :through an association.
I first followed the examples of using checkboxes with HABTM. However,
since I switched my model to user has_many :through, and so I’m pretty
sure that I no longer get the target_lists_ids method above like I would
if I were using HABTM.
And actually, I’m not sure I care about that, since I know I should just
be able to process through the values that I get from the form anyway.
I’m having trouble getting at the values of the checkbox, even though in
my parameter list, I see that they are available:
“@current_job”=>{“target_list_ids”=>[“1”, “3”]}
There’s something very basic that I’m missing here. I want to be able
to grab these values in my controller method and do something like:
params[@current_job][:target_list_ids].each {
//Do what’s necessary to link up my models via the has_many :though
relationship by populating the “through” table.
}
Is my problem that I’m naming my checkboxes using
@current_job[target_list_ids], and this is being literally interpreted
and ending up in the view that way?
I hope this makes sense. I think I am generally confused about how the
check_box and check_box_tag helpers work.
Thanks,
Wes