为什么 fields_for 不迭代传递的 ActiveRecord::Relation?
Why is fields_for not iterating over the passed ActiveRecord::Relation?
我正在尝试设置表单的一部分,用户可以在其中 select 零个或多个医生,但需要提供每个医生的令牌以证明他们与医生交谈过。不管怎样,我有关系在模型上工作,可以设置和获取医生和病人的名单。但是 fields_for
似乎不明白这一点。可能有一些我遗漏的魔法,如果有人能指出来那就太好了。
在控制器中:
def edit
@doctors = User.doctor
super
end
模型中:
class User < ActiveRecord::Base
devise :registerable
has_secure_token
enum role: [:doctor, :admin, :customer, :distributor]
enum salutation: [:mr, :mrs, :ms, :miss, :dr]
has_and_belongs_to_many :doctors, -> { doctor }, {class_name: "User", join_table: :doctors_patients, foreign_key: :patient_id, association_foreign_key: :doctor_id}
has_and_belongs_to_many :patients, {class_name: "User", join_table: :doctors_patients, foreign_key: :doctor_id, association_foreign_key: :patient_id}
end
在视图中:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put,class:"form-horizontal" }) do |f|
...
<%= f.fields_for :doctors, @doctors do |d| %>
<tr>
<td><%= d.check_box :id,{checked: resource.doctors.include? d.object.id} %></td>
<td><%= "#{d.object.salutation.titleize}. #{d.object.last_name}" %></td>
<td><%= "#{d.object.clinic}" %></td>
<td><%= d.text_field :token %></td>
<td><%= link_to 'More','#',class:"btn btn-primary",data:{toggle:"modal",target:"#doctor#{d.object.id}Modal"} %></td>
</tr>
<% end %>
<% end %>
我目前收到一个错误:undefined method 'id' for #<User::ActiveRecord_Relation:0x007faa9392c9b8>
因为整个关系正在 fields_for
中使用,而不是一次使用一个实例。
我只是想实现这个:
Or a collection to be used:
<%= form_for @person do |person_form| %>
...
<%= person_form.fields_for :projects, @active_projects do |project_fields| %>
Name: <%= project_fields.text_field :name %>
<% end %>
...
<% end %>
来自:http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for
将第 <%= f.fields_for :doctors, @doctors do |d| %>
行替换为 <%= f.fields_for :doctor do |d| %>
。
事实证明,如果您想从 fields_for
获得迭代,您的模型中需要 accepts_nested_attributes_for :doctors
。
我正在尝试设置表单的一部分,用户可以在其中 select 零个或多个医生,但需要提供每个医生的令牌以证明他们与医生交谈过。不管怎样,我有关系在模型上工作,可以设置和获取医生和病人的名单。但是 fields_for
似乎不明白这一点。可能有一些我遗漏的魔法,如果有人能指出来那就太好了。
在控制器中:
def edit
@doctors = User.doctor
super
end
模型中:
class User < ActiveRecord::Base
devise :registerable
has_secure_token
enum role: [:doctor, :admin, :customer, :distributor]
enum salutation: [:mr, :mrs, :ms, :miss, :dr]
has_and_belongs_to_many :doctors, -> { doctor }, {class_name: "User", join_table: :doctors_patients, foreign_key: :patient_id, association_foreign_key: :doctor_id}
has_and_belongs_to_many :patients, {class_name: "User", join_table: :doctors_patients, foreign_key: :doctor_id, association_foreign_key: :patient_id}
end
在视图中:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put,class:"form-horizontal" }) do |f|
...
<%= f.fields_for :doctors, @doctors do |d| %>
<tr>
<td><%= d.check_box :id,{checked: resource.doctors.include? d.object.id} %></td>
<td><%= "#{d.object.salutation.titleize}. #{d.object.last_name}" %></td>
<td><%= "#{d.object.clinic}" %></td>
<td><%= d.text_field :token %></td>
<td><%= link_to 'More','#',class:"btn btn-primary",data:{toggle:"modal",target:"#doctor#{d.object.id}Modal"} %></td>
</tr>
<% end %>
<% end %>
我目前收到一个错误:undefined method 'id' for #<User::ActiveRecord_Relation:0x007faa9392c9b8>
因为整个关系正在 fields_for
中使用,而不是一次使用一个实例。
我只是想实现这个:
Or a collection to be used:
<%= form_for @person do |person_form| %> ... <%= person_form.fields_for :projects, @active_projects do |project_fields| %> Name: <%= project_fields.text_field :name %> <% end %> ... <% end %>
来自:http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for
将第 <%= f.fields_for :doctors, @doctors do |d| %>
行替换为 <%= f.fields_for :doctor do |d| %>
。
事实证明,如果您想从 fields_for
获得迭代,您的模型中需要 accepts_nested_attributes_for :doctors
。