簡體   English   中英

為什么我的模型參數被視為未定義的局部變量?:

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

我有一個包含三個模型的應用程序。 主要模型具有_其他兩個模型中的一個,並且次要模型屬於_主要模型。

第一個模型有兩個視圖,每個視圖都有一個表單,用於填充主要模型及其各自次要模型的一部分。 第一個視圖稱為 new,我只是創建了另一個名為 new2 的視圖,因為它們都填充了主模型的數據庫。

在我的兩種形式中,我都有:

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

我沒有用於輔助模型的控制器,在我的主模型控制器中,我定義了參數以包含所有模型的所有屬性。

當我嘗試在開發模式下提交表單時,我收到一個錯誤,指出我的參數是“未定義的局部變量”。

參數在控制器定義。

控制器中的創建是:

 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該方法,您只endif語句。 這意味着當您定義其他方法時,例如editparticipant_params edit ,它們是在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 ,您目前在文件底部而不是此處)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM