簡體   English   中英

模型之間關系的NoMethodError

[英]NoMethodError for relationship between models

我正在建立兩個模型之間的關系。 患者模型和工作清單模型。 我的病人可以屬於工作清單,工作清單可以有很多病人。 在我的患者表格中,我希望能夠從下拉菜單中選擇工作清單。 我在表單中創建了一個方法,但它拋出了一個錯誤。 這是我的代碼:

來自_form.html.erb:

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

來自patient.rb:

class Patient < ActiveRecord::Base
  belongs_to :worklist
end

來自worklist.rb

class Worklist < ActiveRecord::Base
  has_many :patients
end

來自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

這是我得到的錯誤:

NoMethodError in Patients#edit
undefined method `worklist_select'

這是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是一個問題。 直接與db交談的觀點永遠不會好。 這是Controller(或服務)的工作。

在您的控制器中,您需要類似於: @worklists = Worklist.all ,然后在視圖中使用@worklists

這只是一個開始。 您可以發布您的控制器,以便我們可以進一

更新 :現在我們可以更好地了解這種情況。 我們開工吧:

因此,在您的PatientsController中,您需要在create方法中使用它:

@worklists = Worklist.all

然后在_form.html.erb (來自患者文件夾而不是Worklist文件夾)中,您需要:

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

我想如果你改變這個:

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

對於更像這樣的事情:

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

你可能會得到你想要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM