【问题标题】:NoMethodError for relationship between models模型之间关系的 NoMethodError
【发布时间】:2015-03-05 17:07:46
【问题描述】:

我正在我的两个模型之间建立关系。患者模型和工作列表模型。我的患者可以属于一个工作列表,而一个工作列表可以包含许多患者。在我的患者表格中,我希望能够从下拉菜单中选择工作清单。我在表单中创建了一个方法,但它抛出了一个错误。这是我的代码:

来自 _form.html.erb:

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

来自患者.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

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 relationship


    【解决方案1】:

    Worklist.all在视图中有问题。直接与数据库对话的视图从来都不是很好。那是控制器(或服务)的工作。

    在您的控制器中,您需要类似:@worklists = Worklist.all,然后在视图中使用 @worklists

    这只是一个开始。您能否发布您的控制器以便我们进一步提供帮助?

    更新:现在我们对它的发展方向有了更好的了解。让我们这样做:

    所以在您的 PatientController 中,您会希望在您的 create 方法中这样做:

    @worklists = Worklist.all
    

    然后在您的_form.html.erb(来自患者文件夹而不是工作列表文件夹)中,您会想要:

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

    【讨论】:

    • 我刚刚为您发布了我的控制器。谢谢。
    • 抱歉没有澄清。我的意思是发布您的患者控制器。我假设您正在创建一个新患者,然后下拉菜单将选择他们所属的工作列表。因此,在您的 PatientController 中,您需要在 create 方法中使用:@worklists = Worklist.all 然后在您的 _form.html.erb(来自患者文件夹而不是 Worklist 文件夹)中,您需要:
    • 我将 PatientController 与您建议的更改和 _form.html.erb 一起发布在顶部。我现在收到此错误:nil:NilClass 的未定义方法“映射”。这可能是在我的控制器中执行我的 create 方法时吗?
    • 没关系,我必须把它放在我的编辑中,它也能正常工作。非常感谢帮忙。我想我现在清楚地了解如何拨打电话。谢谢!
    【解决方案2】:

    我想如果你只是改变这个:

    &lt;%= worklist_select( :patient, :worklist_id, Worklist.all, :id, :name, {}, { :multiple =&gt; false } ) %&gt;

    类似这样的东西:

    &lt;%= f.select( :worklist_id, Worklist.all, :id, :name, {}, { :multiple =&gt; false } ) %&gt;

    你可能会得到你想要的。

    【讨论】:

    • 至于你得到的具体错误,这是因为你刚刚发明了一种方法,afaict。 Rails 可以免费生成很多好东西,所以我认为值得一试,但不是您希望的特定方法 (worklist_select)。
    • 试了一下,它给了我一个错误:参数数量错误。
    猜你喜欢
    • 1970-01-01
    • 2014-12-02
    • 2012-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 2021-01-24
    相关资源
    最近更新 更多