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
#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@user_invitation
returned nil, but you explicitly tried to access itsinvitation_type
when doingVector.new
even if it's nil. – SreekanthGS@user_invitation
is nil and notinvitation_type
is nil. The error raised is, no methodinvitation_type
for nil class. Here nil class being@user_invitation
. – SreekanthGSunless @user_invitation.present?
in the commented of your original code? – Pavan