Ruby Error:No Method error..nil的未定义方法invitation_type:NilClass

时间:2014-04-23 05:56:54

标签: ruby-on-rails ruby

我正在使用Ruby on Railssite ...我在重定向到另一种形式时遇到问题...我已经做了很多事来解决它但我无法找出代码中的错误。有人请帮我解决..请

错误

←[36m2014-04-23T05:46:13.280376+00:00 app[web.1]:←[0m NoMethodError (undefined m
ethod `invitation_type' for nil:NilClass):
←[36m2014-04-23T05:46:13.280378+00:00 app[web.1]:←[0m   app/controllers/vectors_controller.rb in invited_new

代码:

    class VectorsController < ApplicationController

    def create
        ui = UserInvitation.where(hash_code: session[:hash_code]).first
        @vector = Vector.new(vector_params.merge({user_id: current_user.id, vector_type: ui.invitation_type}))

    respond_to do |format|
      if @vector.save

        ui.completed_registration = true
        ui.save

        session[:hash_code] = nil
        flash[:notice] = "You're all set! Welcome to Availendar!"

        format.html {redirect_to vector_path(@vector.id)}
      else
        flash[:alert] = "Oops, looks like there's been a problem. Please correct it and try again."

        format.html {render action: :invited_new}
      end
    end
  end


  def invited_new
    @user_invitation = UserInvitation.where(hash_code: session[:hash_code]).first
    #raise ArgumentError "Cannot find invitation with hash_code #{session[:hash_code]}" unless @user_invitation.present?

    @vector = Vector.new(user: current_user, vector_type: @user_invitation.invitation_type)
    @vector.build_address
  end
 ////create action///

def创建     ui = UserInvitation.where(hash_code:session [:hash_code])。first     @vector = Vector.new(vector_params.merge({user_id:current_user.id,vendor_type:ui.invitation_type}))     respond_to do | format |       if @ vector.save         ui.completed_registration = true         ui.save         session [:hash_code] = nil         format.html {redirect_to vector_path(@ vector.id)}       其他         format.html {render action :: invite_new}       结束     结束   端

3 个答案:

答案 0 :(得分:3)

在您的invited_new操作中@user_invitation因为显示此错误而显示空白,此处为解决方案

  def invited_new
    @user_invitation = UserInvitation.where(hash_code: session[:hash_code]).first

    unless @user_invitation.blank?    
      @vector = Vector.new(user: current_user, vector_type: @user_invitation.invitation_type)
      @vector.build_address
    end
  end

答案 1 :(得分:1)

您正在unless使用if,错误

也是如此

更改此行

unless @user_invitation.present? #=> false

if @user_invitation.present? #=> true



def invited_new
    @user_invitation = UserInvitation.where(hash_code: session[:hash_code]).first

    if @user_invitation.present?

    @vector = Vector.new(user: current_user, vector_type: @user_invitation.invitation_type)
    @vector.build_address
  end

答案 2 :(得分:1)

好的,让我们一步一步地分解它:

<强>问题

  • 有时此行可能会返回空白数组(找不到任何内容)

    UserInvitation.where(hash_code: session[:hash_code])

  • 这意味着,这将是nil(空白数组中的第一个是nil,对吧!)

    @user_invitation = UserInvitation.where(hash_code: session[:hash_code]).first

  • 现在,它抛出是有道理的(未定义的方法invitation_type为nil:NilClass)

    @user_invitation.invitation_type

<强>解决方案

相反,使用 try ,如名称所示,将尝试获取该属性,或者在nil对象上调用时返回nil,你的方法应该是这样的:

class VectorsController < ApplicationController

  def create
    ui = UserInvitation.where(hash_code: session[:hash_code]).first
    @vector = Vector.new(vector_params.merge({user_id: current_user.id, vector_type: ui.try(:invitation_type)}))

    respond_to do |format|
      if @vector.save
        ui.completed_registration = true
        ui.save
        session[:hash_code] = nil
        flash[:notice] = "You're all set! Welcome to Availendar!"
        format.html {redirect_to vector_path(@vector.id)}
      else
        flash[:alert] = "Oops, looks like there's been a problem. Please correct it and try again."
        format.html {render action: :invited_new}
      end
    end
  end

  def invited_new
    @user_invitation = UserInvitation.where(hash_code: session[:hash_code]).first
    #raise ArgumentError "Cannot find invitation with hash_code #{session[:hash_code]}" unless @user_invitation.present?

    @vector = Vector.new(user: current_user, vector_type: @user_invitation.try(:invitation_type))
    @vector.build_address
  end
end