Has_many关系中的HMBTM复选框

时间:2011-06-30 03:40:19

标签: ruby-on-rails-3 checkbox

我在'events'和'tasks'之间有一对多关系,在show.html.erb中我有以下视图:

<%= form_for([@event, @event.tasks.build]) do |f| %>
 <div class="field">
  <%= f.label :"Task name"  %><br />
  <%= f.text_field :name %>
 </div>
<div class='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 %>

当我实际处理表单上的选择时,它给了我account_id中的1,我不知道为什么会这样

查看开发日志,发现以下内容:

  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)

你知道为什么会这样吗?为什么它将6放入account_id但不更新数据库?

如果您有任何其他意见,请随时发表评论,我是初学者,所以我真的希望学到很多。

提前谢谢

1 个答案:

答案 0 :(得分:0)

<%= check_box_tag "task[account_ids][]", account.id %>

您好,尝试更改字段名称