0

我有一个提交两个模型的表单,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

4

1 回答 1

1

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

@participant= Participant.new
@student_detail= @participant.build_student_detail(participant: @participant)
于 2019-01-18T03:59:19.570 回答