1
votes

I am doing an Ruby on Railssite...i am having a problem while redirecting to another form...i have done many things to fix it but i am not able to find out what is wrong in the code..anyone please help me to fix it ..please

Error

←[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

Code:

    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 create 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)} else format.html {render action: :invited_new} end end end

3
You commented this line #raise ArgumentError "Cannot find invitation with hash_code #{session[:hash_code]}" unless @user_invitation.present?.you did it mistakenly or you wanted it to be like that?Pavan
the problem seems to be in the invited_new action's user_invitation.invitation_type method/attribute call, because invitation_type seems to be nil. Like you said, I think it is useful to check that out. Or even better like RSB pointed out check the whole user_invitation object.jyrkim
Your @user_invitation returned nil, but you explicitly tried to access its invitation_type when doing Vector.new even if it's nil.SreekanthGS
The problem is that @user_invitation is nil and not invitation_type is nil. The error raised is, no method invitation_type for nil class. Here nil class being @user_invitation.SreekanthGS
Firstly to be clear do you have this line unless @user_invitation.present? in the commented of your original code?Pavan

3 Answers

3
votes

In your invited_new action @user_invitation is coming blank due to which it is showing this error, here's the solution

  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
votes

You are using unless for if, so is the error

change this line

unless @user_invitation.present? #=> false

to

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
1
votes

OK, lets try to break it down step by step:

Problem

  • Sometimes this line might return blank array (nothing found)

    UserInvitation.where(hash_code: session[:hash_code])
  • Which means, this will be nil (first of blank array is nil, right !)

    @user_invitation = UserInvitation.where(hash_code: session[:hash_code]).first
  • Now, it makes sense this throws (Undefined method invitation_type for nil:NilClass)

    @user_invitation.invitation_type

Solution

Instead, use try which as the name indicates will try to get the attribute or and simply returns nil when called on a nil object, so your method should look like this:

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