У меня есть одна форма, которая представляет две модели, Participant
и StudentDetail
.
Участник has_one Student_Detail, атрибуты которого вложены в Участника. При попытке получить доступ к форме я получаю NoMethodError
о том, что student_details является неопределенным методом.
В новой версии моего контроллера я попытался изменить имя student_details, чтобы заставить рельсы принять его и встроить его атрибуты в основную модель, Участник.
Вот моя модель,
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
.