HMBTM Checkboxes within Has_many relationship
I have one to many relationship between 'events' and 'tasks' and within show.html.erb I have render the following view:
<%= form_for([@event, @event.tasks.build]) do |f| %>
<div class="field">
<%= f.label :"Task name" %><br />
<%= f.text_field :name %>
</div>
<div clas开发者_开发技巧s='cbx'>
<%= f.label :'Assign To' %><br />
<% for account in Account.find(:all) %>
<%= check_box_tag "task[account_id][]", account.id %>
<%= account.username %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
and when i actually process selection on the form it gives me the 1 in account_id, i have no idea why is this so
Looking at the development log, found the following:
Started POST "/events/2/tasks" for 127.0.0.1 at Thu Jun 30 13:25:59 +1000 2011
Processing by TasksController#create as HTML
Parameters: {"commit"=>"Create Task", "event_id"=>"2","authenticity_token"=>"oSxcZZ/NCH+OyP+UoRSMvmHCP1daPbmJBeJM6tLxhyA=", "utf8"=>"✓",
"task"=>{"name"=>"t", "account_id"=>["6"]}}
Event Load (0.2ms) SELECT "events".* FROM "events" WHERE "events"."id" = 2 LIMIT 1
AREL (0.4ms) INSERT INTO "tasks" ("created_at", "user_id", "event_id", "updated_at", "name", "account_id")
VALUES ('2011-06-30 03:25:59.327287', NULL, 2, '2011-06-30 03:25:59.327287', 't', 1)
Do you have any idea why is this so? why it puts 6 in account_id but doesn't update the database?
And if you have any other comments please feel free to comment, I'm beginner so I really wish to learn a lot.
Thank you in advance
<%= check_box_tag "task[account_ids][]", account.id %>
Hi try to change field name
精彩评论