本文介绍了接受 has_many 关系的嵌套属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我的两个模型类
class Patient < ActiveRecord::Base belongs_to :user, :dependent => :destroy has_many :enrollments, :dependent => :destroy has_many :clients, :through => :enrollments accepts_nested_attributes_for :user accepts_nested_attributes_for :enrollments attr_accessible :user_attributes,:enrollments_attributes, :insurance end class Enrollment < ActiveRecord::Base belongs_to :client belongs_to :patient attr_accessible :client_id, :patient_id, :patient_id, :active end在我的患者表单中,我想要一个多选框,可以将患者分配给客户.有没有办法做到这一点,所以我不必在控制器除了
In my patient form I would like to have a multi select box where a patient can be assigned to clients. Is there a way this can be done so I don't have to have any logic in the controller except for
@patient = Patient.new(params) @patient.save我已经试过了:
<%= patient_form.fields_for :enrollments do |enrollments_fields| %> <tr> <td class="label"> <%= enrollments_fields.label :client_id %>: </td> <td class="input"> <%= enrollments_fields.collection_select(:client_id, @clients, :id, :name, {}, :multiple => true) %> </td> </tr> <% end %>但它只保存了第一个客户端.如果我删除多个部分,它可以运行但我只能选择1个客户端!
But it only saves the first client. If I remove the multiple part, it functions but I can only select 1 client!
select的html值为:
The html value of the select is:
推荐答案我最终做了以下事情:
<%= check_box_tag "patient[client_ids][]", client.id, @patient.clients.include?(client) %>我不确定这是否是最好的方法...任何评论(我必须更新我的模型以包含 attr_accessible :client_ids
I am not sure if this is the best way...any comments (I had to update my model to include attr_accessible :client_ids
更多推荐
接受 has
发布评论