Reputation: 87
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
Upvotes: 1
Views: 1488
Reputation: 5839
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
Upvotes: 1
Reputation: 33542
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
Upvotes: 1
Reputation: 17834
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
Upvotes: 3