简体   繁体   English

simple_form rails 4、设置自动关联

[英]simple_form rails 4, set automatic association

is a little project and I try to associate patient model with consultations.是一个小项目,我尝试将患者模型与咨询联系起来。 one patient has_many :consultations, in my form I have:一位患者 has_many :咨询,以我的形式,我有:

<%= f.association :patient %>

I pass the id parameter from the patient to the action 'new' in this way:我以这种方式将 id 参数从患者传递给操作“新”:

<%= link_to new_consultum_path(:id => @patient.id) %>

And in the view I have:在我看来:

患者编号

  1. How can I make that the f.association field take the correspondent patient_id automatically?我怎样才能让 f.association 字段自动获取通讯者的patient_id?

  2. How can I be sure that the patient_id is the current patient?我如何确定患者 ID 是当前患者?

  3. If I want to hide this field is that ok if I put如果我想隐藏这个字段,如果我把

    instead of代替
  4. Is a better way to do this?有更好的方法来做到这一点吗?

  5. And why in the view shows me # patient:0x007f4e7c32cbd0 ?为什么在视图中显示我 #patient:0x007f4e7c32cbd0 ?

thanks for your help.谢谢你的帮助。

And why in the view shows me # patient:0x007f4e7c32cbd0为什么在视图中向我显示patient:0x007f4e7c32cbd0

This is a Patient object .这是一个Patient对象

It means you need to call an attribute of this object - EG @patient.name .这意味着您需要调用此对象的一个属性- EG @patient.name

-- ——

f.association field take the correspondent patient_id automatically f.association field自动取对应的patient_id

This might help: 可能有帮助:

It looks like Organization model doesn't have any of these fields: [ :to_label , :name , :title , :to_s ] so SimpleForm can't detect a default label and value methods for collection.看起来 Organization 模型没有以下任何字段: [ :to_label , :name , :title , :to_s ] 所以SimpleForm无法检测集合的默认标签和值方法。 I think you should pass it manually.我认为你应该手动传递它。

#app/models/patient.rb
class Patient < ActiveRecord::Base
   def to_label 
      "#{name}"
   end
end

Apparently, you need to have either title , name or to_label methods in your model in order for f.association to populate the data.显然,您的模型中需要有titlenameto_label方法,以便f.association填充数据。

- ——

How can I be sure that the patient_id is the current patient?我如何确定患者 ID 是当前患者?

If you're having to verify this, it suggests inconsistencies with your code's structure.如果您必须验证这一点,则表明与您的代码结构不一致。 If you need the patient_id to be set as the current patient , surely you could set it in the controller:如果您需要将patient_id设置为current patient ,您当然可以在控制器中设置它:

#app/controllers/consultations_controller.rb
class ConultationsController < ApplicationController
   def create
      @consultation = Constultation.new
      @consultation.patient = current_patient
      @consultation.save
   end
end

I can provide more context if required.如果需要,我可以提供更多上下文。

You want to associate consultations to patients using fields_for, which is similar to form_for, but does not build the form tags.您希望使用 fields_for 将咨询与患者相关联,这与 form_for 类似,但不构建表单标签。

It you start with your patient object, you can iterate through the consultation associations binding it to form fields as you go.如果您从您的患者对象开始,您可以遍历咨询关联,将其绑定到表单字段。

it would look something like this它看起来像这样

<%= form_for @patient do |patient_form| %>
    <% patient_form.text_field :any_attribute_on_patient %>
    <% @patient.consultations.each do |consultation| %>
        <%= patient_form.fields_for consultation do |consultation_fields| %>
            <% consultation_fields.text_field :any_attribute_on_consulatation %>
        <% end %>         
    <% end %>
<% end %>

Sorry, the code may not be exactly right.抱歉,代码可能不完全正确。

Check out the docs for field_for here 在此处查看 field_for 的文档

You will also have to set accepts_nested_attributes_for consultations on patient.您还必须为患者设置accepts_nested_attributes_for咨询。 When you set accepts_nested_forms_for, Rails will automatically update the associated consultations object to the patient and save any edits you have made.当您设置 accepts_nested_forms_for 时,Rails 将自动更新与患者相关的咨询对象并保存您所做的任何编辑。 You DEFINITELY want to use accepts_nested_attributes_for most nested form handling of this type.您绝对希望使用 accepts_nested_attributes_for 大多数这种类型的嵌套表单处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM