为什么我的参数会得到未定义的变量? Fields_for多个模型参数

时间:2019-01-14 21:34:26

标签: ruby-on-rails parameters fields-for undefined-variable

我的应用程序出现未定义的变量错误,声称我的参数是未定义的变量。这表明我改用了partner_path?

我正在开发具有多个模型的应用程序。有一种形式是使用fields_for来填充与主要模型无关的模型的数据。

测试时,我使用的是主要模型的控制器来处理表单提交,并定义参数以包含两个模型的属性。

这是使用fields_for的正确方法吗?

我将在以后通过手机发布代码时添加我的代码。

使用代码更新: 这是我的表格:

<%= 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 %>

这是我的控制器中的相关代码:

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

当我在本地服务器上对此进行测试时,出现以下错误:

  

未定义的局部变量或方法`participant_params'是什么意思?   参与者路径参与者路径参与者路径

谢谢!

0 个答案:

没有答案