【问题标题】:How to resolve a NoMethodError on a controller dealing with multiple models?如何解决处理多个模型的控制器上的 NoMethodError?
【发布时间】:2019-01-18 03:44:48
【问题描述】:

我有一个提交两个模型的表单,ParticipantStudentDetail

Participant has_one Student_Detail,其属性嵌套在 Participant 中。尝试访问表单时,我收到NoMethodError 指出 student_details 是一个未定义的方法。

在我的控制器的 def new 中,我尝试更改 student_details 的名称以让 rails 接受它并将其属性构建到主要模型 Participant 中。

这是我的模型,

class Participant < ApplicationRecord

  validates :last_name, presence: true
  # validates :gender, inclusion: { in: %w(male female) }
  validates :nationality, presence: true
  validates :phone, presence: true
  has_one :volunteer_detail, :dependent => :destroy
  accepts_nested_attributes_for :volunteer_detail,   :allow_destroy => :true

  has_one :student_detail, :dependent => :destroy
  accepts_nested_attributes_for :student_detail,   :allow_destroy => :true

end

这是我的控制器:

class ParticipantsController < ApplicationController

  def new
    @participant= Participant.new
    @studentdetails= participant.student_details.build(participant: @participant)
  end


  def create
    @participant = Participant.create(participant_params)
    @participant.save
    if @participant.save
      flash[:success] = "Successfully Registered!"        

      #email notifes admin of new registration
      NotifyMailer.notify_email(@participant).deliver_later

      redirect_to '/signup'
    end
  end

  def edit
  end

  def update
  end

  def show
  end

  def index
  end

  private

  def participant_params
    params.require(:participant).permit(:first_name, :last_name, :gender, :email, :birthdate, :phone, 
      :street_name, :city, :state, :zip, student_details_attributes: [:nationality, :religion, :need_ride, 
    :has_spouse, :spouse_name, :english_level, :expectations, :length_of_stay, :exact_length, :volunteer_id, 
    :matched, :returned_home, :participant_id])
  end

end

这是我的服务器日志:

Started GET "/signup" for 127.0.0.1 at 2019-01-17 21:31:29 -0600
Processing by ParticipantsController#new as HTML
Completed 500 Internal Server Error in 19ms (ActiveRecord: 2.0ms)

NoMethodError (undefined method `student_details' for #<Class:0x00000000090c9af0>):

app/controllers/participants_controller.rb:4:in `new'
Rendering C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
Rendering C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
Rendered C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (3.5ms)
Rendering C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
Rendered C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.5ms)
Rendering C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
Rendered C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.5ms)
Rendered C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (776.1ms)

我试图让我的表单提交并填充两个表,student_detail 属性可由Participant 模型访问。

【问题讨论】:

  • 当然student_details是未定义的,因为关联是has_one: student_detail,单数

标签: ruby-on-rails controller nomethoderror accepts-nested-attributes


【解决方案1】:

在您的控制器(新)中,您缺少“@”并使用 student_detail,因为您的模型使用 student_detail(记住 has_one 不是 has_many)

@participant= Participant.new
@student_detail= @participant.build_student_detail(participant: @participant)

【讨论】:

  • 谢谢 - 没有看到丢失的 @。仍然得到 NoMethodError,但这次我得到“nil:NilClass 的未定义方法‘build’”,据我了解,这意味着这两个模型实际上并没有连接——对吗?我不知道为什么我会得到这个错误。
  • 对不起 has_one 必须使用 build_student_detail 而不是 student_detail.build,我只是更正我的答案
猜你喜欢
  • 1970-01-01
  • 2011-11-21
  • 2014-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多