简体   繁体   English

模型之间关系的NoMethodError

[英]NoMethodError for relationship between models

I am setting up a relationship between two of my models. 我正在建立两个模型之间的关系。 A Patient Model and Worklist Model. 患者模型和工作清单模型。 My patient can belong to a worklist and a worklist can have many patients. 我的病人可以属于工作清单,工作清单可以有很多病人。 In my form for patients I want to be able to select the worklist from a dropdown menu. 在我的患者表格中,我希望能够从下拉菜单中选择工作清单。 I created a method in the form but its throwing an error. 我在表单中创建了一个方法,但它抛出了一个错误。 Here is my code: 这是我的代码:

from _form.html.erb: 来自_form.html.erb:

<div class="field">
  <%= f.label :worklist_id %>
  <%= f.collection_select :worklist_id, @worklists, :id , :name %>
</div>

from patient.rb: 来自patient.rb:

class Patient < ActiveRecord::Base
  belongs_to :worklist
end

from worklist.rb 来自worklist.rb

class Worklist < ActiveRecord::Base
  has_many :patients
end

from schema.rb 来自schema.rb

  create_table "patients", force: :cascade do |t|
    t.string   "lastname"
    t.string   "middlename"
    t.string   "firstname"
    t.date     "birthdate"
    t.string   "sex"
    t.string   "ethnicity"
    t.string   "uniqueid"
    t.string   "accountnumber"
    t.string   "medicaidid"
    t.string   "medicareid"
    t.string   "ssn"
    t.datetime "created_at",    null: false
    t.datetime "updated_at",    null: false
    t.integer  "worklist_id"
  end

And this is the error I am getting: 这是我得到的错误:

NoMethodError in Patients#edit
undefined method `worklist_select'

Here is the PatientController: 这是PatientController:

class PatientsController < ApplicationController
  before_action :set_patient, only: [:show, :edit, :update, :destroy]

  # GET /patients
  # GET /patients.json
  def index
    @patients = Patient.all
  end

  # GET /patients/1
  # GET /patients/1.json
  def show
  end

  # GET /patients/new
  def new
    @patient = Patient.new
  end

  # POST /patients
  # POST /patients.json
  def create
    @patient = Patient.new(patient_params)
    @worklists = Worklist.all

    respond_to do |format|
      if @patient.save
        format.html { redirect_to @patient, notice: 'Patient was successfully created.' }
        format.json { render :show, status: :created, location: @patient }
      else
        format.html { render :new }
        format.json { render json: @patient.errors, status: :unprocessable_entity }
      end
    end
  end

  # GET /patients/1/edit
  def edit
  end

  # PATCH/PUT /patients/1
  # PATCH/PUT /patients/1.json
  def update
    respond_to do |format|
      if @patient.update(patient_params)
        format.html { redirect_to @patient, notice: 'Patient was successfully updated.' }
        format.json { render :show, status: :ok, location: @patient }
      else
        format.html { render :edit }
        format.json { render json: @patient.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /patients/1
  # DELETE /patients/1.json
  def destroy
    @patient.destroy
    respond_to do |format|
      format.html { redirect_to patients_url, notice: 'Patient was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

    def import
      Patient.import(params[:file])
      redirect_to patients_path, notice: "Patients Added Successfully"
    end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_patient
      @patient = Patient.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def patient_params
      params.require(:patient).permit(:lastname, :middlename, :firstname, :birthdate, :sex, :ethnicity, :uniqueid, :accountnumber, :medicaidid, :medicareid, :ssn, :worklist_id)
    end
end

Worklist.all in the view is a problem. 在视图中的Worklist.all是一个问题。 The view talking to the db directly is never good. 直接与db交谈的观点永远不会好。 That is the Controller's (or a Service's) job. 这是Controller(或服务)的工作。

In your controller, you would want something like: @worklists = Worklist.all , then use @worklists in the view. 在您的控制器中,您需要类似于: @worklists = Worklist.all ,然后在视图中使用@worklists

This is just a start though. 这只是一个开始。 Can you post your controller so we can help further? 您可以发布您的控制器,以便我们可以进一

UPDATE : Now we have a better idea of where this is going. 更新 :现在我们可以更好地了解这种情况。 Lets do this: 我们开工吧:

So in your PatientsController, you would want this in your create method: 因此,在您的PatientsController中,您需要在create方法中使用它:

@worklists = Worklist.all

Then in your _form.html.erb (from the patient folder NOT the Worklist folder), you would want: 然后在_form.html.erb (来自患者文件夹而不是Worklist文件夹)中,您需要:

<%= f.label :worklist_id %>
<%= f.collection_select :worklist_id, @worklists, :id , :name %>

I think if you just change this: 我想如果你改变这个:

<%= worklist_select( :patient, :worklist_id, Worklist.all, :id, :name, {}, { :multiple => false } ) %>

To something more like this: 对于更像这样的事情:

<%= f.select( :worklist_id, Worklist.all, :id, :name, {}, { :multiple => false } ) %>

You might get what you want. 你可能会得到你想要的。

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

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