【问题标题】:has_many :throught not INSTERING INTO database from formhas_many :通过不从表单插入数据库
【发布时间】:2015-09-02 22:14:37
【问题描述】:

我已经尝试了所有类似问题的解决方案,但没有弄清楚这一点。

我有一个 has_many :through 'Clinician' 和 'Patient' 之间的关系以及一个加入模型'CareGroupAssignment'。我希望有一个patient 能够有多个clinicians 与之关联,clinicians 将有多个patients

当我提交表单时,记录如下: Started POST "/patients" for 127.0.0.1 at 2015-09-02 14:56:38 -0700 Processing by PatientsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"XXX=", "patient"=>{"user_attributes"=>{"email"=>"allencentre", "password"=>"[FILTERED]"}, "first_name"=>"Allen", "last_name"=>"Centre", "birth_date(1i)"=>"2002", "birth_date(2i)"=>"9", "birth_date(3i)"=>"2", "clinician_ids"=>["85", "87"], "patient_deceased"=>"0", "patient_archived"=>"0"}, "button"=>""}

所以"clinician_ids"=>["85", "87"] 是从表单中传递过来的。

有一个INSERT INTO "users" ("email", .... 和一个INSERT INTO "patients" ("bir....,但care_group_assignments 的数据没有。

[36mCareGroupAssignment Load (0.4ms)[0m [1mSELECT "care_group_assignments".* FROM "care_group_assignments" WHERE "care_group_assignments"."patient_id" = $1[0m [["patient_id", 199]]

clinician.rb(简体)

class Clinician < ActiveRecord::Base
    belongs_to :care_group

    has_many :patients ,:through=> :care_group_assignments
    has_many :care_group_assignments, :dependent => :destroy

    belongs_to :user
    accepts_nested_attributes_for :user,  :allow_destroy => true
end

患者.rb

class Patient < ActiveRecord::Base
    belongs_to :care_group

    has_many :clinicians ,:through=> :care_group_assignments
    has_many :care_group_assignments

    belongs_to :user
    accepts_nested_attributes_for :user,  :allow_destroy => true
end

care_group_assignments.rb

class CareGroupAssignment < ActiveRecord::Base
    belongs_to :clinician
    belongs_to :patient
end

这是遵循Railscasts PRO #17- HABTM Checkboxes 的示例,至少开始收集数据并正确设置模型。下面是 RailsCast 中描述的带有每个临床医生复选框的表单,复选框显示并且数据已发送但未存储(无法弄清楚原因)。

患者new.html.erb表格

<%= form_for @patient do |form| %>

  <%= form.fields_for :user do |builder| %>
   <div class="form-group">
      <%= builder.label "Email or Username" %>
      <%= builder.text_field :email, class: "form-control" %>
    </div>
    <div class="form-group">
      <%= builder.label :password %>
      <%= builder.password_field :password, class: "form-control" %>
    </div>
  <% end %>

  <div class="form-group">
    <%= form.label :first_name %>
    <%= form.text_field :first_name, class: "form-control", placeholder: "First name" %>
  </div>

  <div class="form-group">
    <%= form.label :last_name %>
    <%= form.text_field :last_name, class: "form-control", placeholder: "Last name" %>
  </div>

  <div class="form-group">
    <% Clinician.where(care_group_id: @care_group.id).each do |clinician| %>
      <%= check_box_tag "patient[clinician_ids][]", clinician.id, @patient.clinician_ids.include?(clinician.id), id: dom_id(clinician) %>
      <%= label_tag dom_id(clinician), clinician.full_name %><br>
    <% end %>
  </div>

  <%= form.button 'Create Patient', class: "btn btn-u btn-success" %>
<% end %>

此方法至今无法保存clinicianpatient的关联。

patient_controller.rb

def new
  @patient = Patient.new
  @user = User.new
  @patient.build_user

  @care_group = current_clinician.care_group
end

def create
  @patient = Patient.create(patient_params)
  @patient.care_group = current_clinician.care_group

  if @patient.save
    redirect_to patient_path(@patient), notice: "New patient created!"
  else
    render "new"
  end
end

def show
 @patient = Patient.find_by(id: params["id"])
end

private
 def patient_params
  params.require(:patient).permit({:clinician_ids => [:id]},:first_name,:last_name,:user_id,:birth_date, user_attributes: [ :email, :password, :patient_id, :clinician_id ])
 end

我计划在患者show 页面上显示与patient 关联的clinicians

患者show.html.erb

<strong>Shared with:</strong>
 <% @patient.clinicians.each do |clinician| %>
  <%= clinician.full_name %><
 <% end %>

如果我为数据库播种,但由于数据似乎没有被存储,所以什么都没有显示。

Rails 4.1.8,红宝石 2.2.1p85,PostgreSQL

谢谢

【问题讨论】:

    标签: ruby-on-rails ruby forms has-many-through strong-parameters


    【解决方案1】:

    我相信您的问题是控制器中的这一行:

    params.require(:patient).permit({:clinician_ids => [:id]}...
    

    应该是:

    params.require(:patient).permit({:clinician_ids => []}...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-20
      • 1970-01-01
      相关资源
      最近更新 更多