I have an app with three models. The primary model has_one of the other two models, and and the secondary models belongs_to the primary model.
The first model has two views, each with a form that populates the primary model and part of its respective secondary model. The first view is called new, and I simply created another view entitled new2, as they both populate the primary model's database.
In both my forms, I have:
<%= f.fields_for @primarymodel.secondary_model do |secondary_model_field| %>
I don't have a controller for the secondary models, and in my primary model controller I have defined the params to include all attributes from all thee models.
When I attempt to submit the forms in dev mode, I get an error that my params are an "undefined local variable."
The params are defined in the controller.
The create in the controller is:
def create
@primarymodel= Primarymodel.create(primarymodel_params)
@primarymodel.save
end
Is there a reason that params would show up as an undefined variable?
Update: Here is my whole controller
class ParticipantsController < ApplicationController
def new
@participant= Participant.new
end
def new2
# @participant= Participant.new
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_now
redirect_to '/signup'
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, :baptism_date, :baptism_importance, :christian_story, :questions, :nationality, :religion, :need_ride,
:has_spouse, :spouse_name, :english_level, :expectations, :length_of_stay, :exact_length, :volunteer_id, :matched, :returned_home)
end
end
end
Here is the server error:
Started POST "/participants" for 127.0.0.1 at 2019-01-14 21:15:38 -0600
ActiveRecord::SchemaMigration Load (0.5ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by ParticipantsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"oMlCbilHzH18I9WmwqweShDLa3vhPy8d+t4cdsU8fSR2ReijiwMhIOmdi0vzQfdsO4rwD3OqGRR7ZvLshOuFtg==", "participant"=>{"first_name"=>"J", "last_name"=>"W", "gender"=>"Male", "email"=>"[email protected]", "phone"=>"xxxxxxxxxx", "street_name"=>"41 Belling", "city"=>"Nashville", "state"=>"Tennessee", "zip"=>"", "role"=>"Reader", "student_details"=>{"nationality"=>"", "religion"=>"", "birthdate"=>"", "need_ride"=>"Yes", "has_spouse"=>"married", "spouse_name"=>"", "english_level"=>"Low", "expectations"=>"", "length_of_stay"=>"Less than 1 Year", "exact_length"=>""}}, "commit"=>"Register"}
Completed 500 Internal Server Error in 445ms (ActiveRecord: 0.0ms)
NameError (undefined local variable or method `participant_params' for #<ParticipantsController:0x0000000008915228>
Did you mean? participant_path
participant_url
participants_path):
app/controllers/participants_controller.rb:11:in `create'
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 (8.0ms)
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 (2.8ms)
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 (2.9ms)
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 (1881.1ms)
This is the 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
has_one :reader_detail
end
Take a look at your
createmethod:As you can see, you don't
endthe method, you only have anendfor theifstatement. This means that when you define other methods likeeditandparticipant_paramsthey are defined within thecreatemethod.Change your
createmethod so that youendboth theifstatement and the method like so:(Notice the last
end, you currently have that at the bottom of your file rather than here)