接受 has

编程入门 行业动态 更新时间:2024-10-27 20:30:38
本文介绍了接受 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

本文发布于:2023-10-18 06:05:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1503350.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!