NoMethodError in SessionsController#create in ruby on rails -
class sessionscontroller < applicationcontroller def create auth = request.env["omniauth.auth"] user = user.find_by_provider_and_uid(auth["provider"],auth["uid"]) || user.create_with_omniauth(auth) session[:user_id]=user.id redirect_to("/sessions/sign") end def sign end end
this in user model
class user < activerecord::base attr_accessible :name, :provider, :uid def self.create_with_omniauth(auth) create! |user| user.provider=auth["provider"] user.uid=auth["uid"] user.name=auth["user_info"]["name"] end end end
error:
undefined method '[]' nil:nilclass
when sign in through facebook above error
you need make sure following exists , not nil
auth = request.env["omniauth.auth"]
you do
auth = request.env["omniauth.auth"] if auth # stuff else # error handler end
or in model check:
def self.create_with_omniauth(auth) return unless auth create! |user| user.provider = auth["provider"] user.uid = auth["uid"] user.name = auth["user_info"]["name"] end end
finally can use try
method deal nil
values, this:
auth.try(:[], 'provider')
if auth
nil
, return nil
, otherwise return value key provider
Comments
Post a Comment