原始问题
两个资源:users
和animals.
创建用户时,客户端会选中复选框以说明他们拥有多少动物。
提交用户表单时,不仅应该创建新的user
记录,还应该在animal_users
富连接表中创建一组记录来表示每个复选框客户选择了。
我认为问题是我没有正确指定表单中的复选框部分。我查看了checkbox_tag API,Rails Guides on Forms以及许多网站和stackOverflow帖子。
提前致谢,代码如下:
原始代码(进一步向下回答代码) :
模型:
#models/user.rb
class User < ActiveRecord::Base
has_many :animals, through: :animal_users
has_many :animal_users
accepts_nested_attributes_for :animal_users, allow_destroy: true
end
#models/animal.rb
class Animal < ActiveRecord::Base
has_many :users, through: :animal_users
has_many :animal_users
end
#models/animal_user.rb
class AnimalUser < ActiveRecord::Base
belongs_to :animal
belongs_to :user
end
user
表格:
#views/users/_form.html.erb
<%= form_for(@user) do |f| %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div>
<% Animal.all.each do |animal| %>
<label>
<%= check_box_tag "user[animal_ids][]", animal.id, f.object.animals.include?(animal) %>
<%= animal.animal_name %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
users_controller.rb
def user_params
params.require(:user).permit(:name, animal_users_attributes: [:_destroy, :id, :user_id, :animal_id])
end
在此处回答代码:
模型:
#models/user.rb
class User < ActiveRecord::Base
has_many :animals, through: :animal_users
has_many :animal_users
end
#models/animal.rb
class Animal < ActiveRecord::Base
has_many :users, through: :animal_users
has_many :animal_users
end
#models/animal_user.rb
class AnimalUser < ActiveRecord::Base
belongs_to :animal
belongs_to :user
end
user
表格:
#views/users/_form.html.erb
<%= form_for(@user) do |f| %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
# Checkbox part of the form that now works!
<div>
<% Animal.all.each do |animal| %>
<%= check_box_tag "user[animal_ids][]", animal.id, f.object.animals.include?(animal) %>
<%= animal.animal_name %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
users_controller.rb
def user_params
params.require(:user).permit(:name, animal_ids: [])
end
只是为了完整起见,这是在表单提交时传递到服务器的内容:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xyz=", "user"=>{"name"=>"Neil", "animal_ids"=>["1", "3"]}, "commit"=>"Create User"}
Animal Load (0.2ms) SELECT "animals".* FROM "animals" WHERE "animals"."id" IN (1, 3)
(0.1ms) begin transaction
SQL (0.2ms) INSERT INTO "users" ("created_at", "name", "updated_at") VALUES (?, ?, ?) [["created_at", "2015-03-26 15:29:00.478767"], ["name", "Neil"], ["updated_at", "2015-03-26 15:29:00.478767"]]
SQL (0.1ms) INSERT INTO "animal_users" ("animal_id", "created_at", "updated_at") VALUES (?, ?, ?) [["animal_id", 1], ["created_at", "2015-03-26 15:29:00.479833"], ["updated_at", "2015-03-26 15:29:00.479833"]]
SQL (0.0ms) INSERT INTO "animal_users" ("animal_id", "created_at", "updated_at") VALUES (?, ?, ?) [["animal_id", 3], ["created_at", "2015-03-26 15:29:00.480644"], ["updated_at", "2015-03-26 15:29:00.480644"]]
SQL (0.1ms) UPDATE "animal_users" SET "updated_at" = ?, "user_id" = ? WHERE "animal_users"."id" = 6 [["updated_at", "2015-03-26 15:29:00.481362"], ["user_id", 8]]
SQL (0.1ms) UPDATE "animal_users" SET "updated_at" = ?, "user_id" = ? WHERE "animal_users"."id" = 7 [["updated_at", "2015-03-26 15:29:00.482062"], ["user_id", 8]]
(2.4ms) commit transaction
答案 0 :(得分:34)
collection_check_boxes是更好的选择:
<%= f.collection_check_boxes(:animal_ids, Animal.all, :id, :name) do |animal| %>
<%= animal.label { animal.check_box } %>
<% end %>
答案 1 :(得分:33)
<% Animal.all.each do |animal| %>
<label>
<%= check_box_tag "user[animal_ids][]", animal.id, f.object.animals.include?(animal) %>
<%= animal.name %>
</label>
<% end %>
并且您必须在控制器参数上接受带有一组id的哈希,类似于
{ animal_ids: [] }
也许阅读这可以帮助: http://millarian.com/programming/ruby-on-rails/quick-tip-has_many-through-checkboxes/
答案 2 :(得分:0)
另一个对我来说更容易理解的使用 collection_check_boxex 的例子:
<%= f.collection_check_boxes(:animal_ids, Animal.all, :id, :name) do |animal| %>
<div class="form-check">
<%= animal.check_box(class: "form-check-input") %>
<%= animal.label(class: "form-check-label") %>
</div>
<% end %>
它适用于 Rails 6