Why am I getting undefined variable for my params? Fields_for multiple model params

58 Views Asked by At

I am getting an undefined variable error with my app, claiming that my params is an undefined variable. It is suggesting that I use participant_path instead?

I am working on an app that has multiple models. There is a form that is using fields_for to fill in data for the a model which is hanging off the primary model.

When testing it I am using the primary model’s controller to process the form submission and defining the params to include both models’ attributes.

Is this the correct way to use fields_for?

I will add my code later, as I am posting this from my phone.

UPDATE with code: Here is my form:

<%= form_for @participant, url: {action: "create"} do |f| %>
<%= f.label :first_name, 'First:' %>
                        <%= f.text_field :first_name %>
                        <%= f.label :last_name, 'Last:' %>
                        <%= f.text_field :last_name %>
                        <%= f.label :gender, 'Sex:' %>                       
                        <%= f.select :gender, ['Male', 'Female'] %>
                        <br>
                        <br>
                        <%= f.label :email, 'Email:' %>
                        <%= f.text_field :email %>
                        <%= f.label :phone, 'Phone:' %>
                        <%= f.text_field :phone %>

                        <%= f.label :street_name, 'Street Number & Name:' %>
                        <%= f.text_field :street_name %>
                        <%= f.label :city, 'City:' %>
                        <%= f.text_field :city %>
                        <%= f.label :state, 'State:' %>
                        <%= f.text_field :state %>
                        <%= f.label :zip, 'Zip:' %>
                        <%= f.text_field :zip %>
                        <%= f.hidden_field :role, :value => 'Reader' %>
             <%= f.fields_for @participant.student_details do |student_detail_field| %>
                        <%= f.label :nationality, 'Nationality:' %>
                        <%= student_detail_field.text_field :nationality %>
                        <%= f.label :religion, 'Religion:' %>
                        <%= student_detail_field.text_field  :religion  %>
                        <%= f.label :birthdate, 'Birthdate:' %>
                        <%= student_detail_field.date_field :birthdate %>
                        <%= f.label :need_ride, 'Do you need help getting to the building?' %>
                        <%= student_detail_field.select :need_ride, ['Yes','No'] %> 
                        <br>
                        <br>
                        <%= f.label :has_spouse, 'Marital Status:' %>
                        <%= student_detail_field.select :has_spouse, ['married', 'single'] %>
                        <br>
                        <br>
                        <%= f.label :spouse_name, "If married, spouse's name:" %>
                        <%= student_detail_field.text_field  :spouse_name %>
                        <%= f.label :english_level, 'English Level:' %>
                        <%= student_detail_field.select :english_level, ['Low', 'Medium Low', 'Medium', 'Medium High', 'High']  %>
                        <%= f.label :expectations, 'What are your expectations for the program?:' %>
                        <%= student_detail_field.text_area :expectations %>
                        <%= f.label :length_of_stay, 'About how long will you be in Nashville?:' %>
                        <%= student_detail_field.select :length_of_stay, ['Less than 1 Year', '1 Year', '1-2 Years', '2-3 Years', '3+ Years'] %>
                        <%= f.label :exact_length, 'Please tell us exactly how long you plan to be in town:' %>
                        <%= student_detail_field.text_area :exact_length %>
            <% end %>

<br>
<br>

<%= f.submit 'Register' %> 

<% end %>

Here is the relevant code in my controller:

def new
    @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 participant_params
    params.require(:participant).permit(:first_name, :last_name, :gender, :email, :birthdate, :phone, :street_name, :city, :state, :zip, :nationality, :religion, :need_ride, 
      :has_spouse, :spouse_name, :english_level, :expectations, :length_of_stay, :exact_length, :volunteer_id, :matched, :returned_home)

  end

When I test this on the local server, I get this error:

undefined local variable or method `participant_params' Did you mean? participant_path participant_url participants_path

Thanks in advance!

0

There are 0 best solutions below