如何解决处理多个模型的控制器上的 NoMethodError?
How to resolve a NoMethodError on a controller dealing with multiple models?
我有一个提交两个模型的表单,Participant
和 StudentDetail
。
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_detail,因为你的模型使用student_detail(记住has_one而不是has_many)
@participant= Participant.new
@student_detail= @participant.build_student_detail(participant: @participant)
我有一个提交两个模型的表单,Participant
和 StudentDetail
。
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_detail,因为你的模型使用student_detail(记住has_one而不是has_many)
@participant= Participant.new
@student_detail= @participant.build_student_detail(participant: @participant)