为什么我的模型参数被认为是未定义的局部变量?:

Why are my model params being considered an undefined local variable?:

我有一个包含三个模型的应用程序。其他两个模型的主要模型 has_one,以及主要模型的次要模型 belongs_to。

第一个模型有两个视图,每个视图都有一个表单,填充主要模型及其各自次要模型的一部分。第一个视图称为 new,我只是创建了另一个名为 new2 的视图,因为它们都填充了主模型的数据库。

在我的两种形式中,我都有:

  <%= f.fields_for @primarymodel.secondary_model do |secondary_model_field| %>

我没有用于辅助模型的控制器,在我的主模型控制器中,我定义了参数以包含所有模型的所有属性。

当我尝试在开发模式下提交表单时,我收到一条错误消息,提示我的参数是 "undefined local variable."

参数在控制器中定义

控制器中的创建是:

 def create
     @primarymodel= Primarymodel.create(primarymodel_params)
    @primarymodel.save

end

params 显示为未定义变量是否有原因?

更新: 这是我的整个控制器

    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

这是服务器错误:

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"=>"j@xxxx.com", "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)

这是型号:

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

看看你的create方法:

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

如您所见,您没有 end 方法,只有 end 用于 if 语句。这意味着当您定义 editparticipant_params 等其他方法时,它们是在 create 方法中定义的。

更改您的 create 方法,以便您 end 同时使用 if 语句和方法,如下所示:

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
 end

(请注意最后一个 end,您目前将其放在文件底部而不是此处)