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



我有一个有三种型号的应用程序。主模型具有其他两个模型中的一个,并且辅助模型属于主模型。

第一个模型有两个视图,每个视图都有一个表单,用于填充主模型及其相应辅助模型的一部分。第一个视图被称为new,我只是创建了另一个名为new2的视图,因为它们都填充了主模型的数据库。

在我的两种表格中,我都有:

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

我没有用于次要模型的控制器,在我的主要模型控制器中,我定义了params,以包括所有电子模型的所有属性。

当我尝试在dev模式下提交表单时,我得到一个错误,我的params是一个"未定义的局部变量">

参数是在控制器中定义的

控制器中的创建是:

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方法,您只有if语句的end。这意味着,当您定义其他方法(如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,您当前将其放在文件底部,而不是此处)

相关内容

  • 没有找到相关文章

最新更新