ruby-on-railscontrollernomethoderroraccepts-nested-attributes

How to resolve a NoMethodError on a controller dealing with multiple models?


I have a single form that submits two models, Participant and StudentDetail.

Participant has_one Student_Detail, whose attributes are nested within Participant. When trying to access the form, I am getting a NoMethodError stating that student_details is an undefined method.

In my controller's def new, I have tried to alter the name of student_details to get rails to accept it and build its attributes into the primary model, Participant.

Here is my model,

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

Here is my controller:

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

Here is my server log:

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)

I'm trying to have my form submit and populate both tables, with the student_detail attributes accessible by the Participant model.


Solution

  • in your controller (new) you missing '@' and use student_detail, since your model using student_detail (remember has_one not has_many)

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