Search code examples
ruby-on-railsrubykoala

Rails: undefined method `name' for Hash


Update: part of the problem might have been solved by accessing the data like

first_name = graphdata[:first_name]

instead of like

first_name = userdata.first name

but when I go to save it, I'm now getting this error

 undefined method `save!' for #<Class:0x00000102dbe068>

Original problem

I'm sure there's probably multiple problems with this code.

I'm playing around with a clone of a Rails app https://github.com/banane/sample-koala-rails-app that uses Koala to authenticate with Facebook and get user data. It does this with the callback method in the home_controller.rb below.

I'm trying to save the authentication token and the user data to a User model that I created.

In the callback method below, I inserted this code

     user = User.oath(session[:access_token], @user_info)   ### my code/ likely wrong
     session[:user_id] = user.id                            ### my code/ likely wrong

to try to save the data to the User model, using the 'oath' class method I made on the User model (see below). Note,I'm not even sure if I can pass that instance variable @user_info into the method...

When I test it, the error I get is

undefined method `name' for #<Hash:0x00000103457d70>

and the trace says

app/models/user.rb:8:in `oath'
  app/controllers/home_controller.rb:30:in `callback'

Here's the code

User.rb

def self.oath(access_token, userdata)      #my code/ likely wrong


    name = userdata.name
    first_name = userdata.first_name
    last_name = userdata.last_name
    username = userdata.username
    gender = userdata.gender
    email = userdata.email
    access_token = access_token
    save!
  end

Home_controller.rb (towards the end of the callback method I try to save the user data)

def callback
    if params[:code]
        # acknowledge code and get access token from FB
          session[:access_token] = session[:oauth].get_access_token(params[:code])
        end     

         # auth established, now do a graph call:

        @api = Koala::Facebook::API.new(session[:access_token])
        begin
            @user_info = @api.get_object("me")
            @graph_data = @api.get_object("/me/statuses", "fields"=>"message")
        rescue Exception=>ex
            puts ex.message
        end

        user = User.oath(session[:access_token], @user_info)  #my code/likely wrong
        session[:user_id] = user.id


        respond_to do |format|
         format.html {   }           
        end


    end

The @user_info has all this data in it

{"id"=>"8331884858", "name"=>"Michael MYLASTNAME", "first_name"=>"Michael", "last_name"=>"MYLASTNAMe", "link"=>"http://www.facebook.com/myusername", "username"=>"myusername", "gender"=>"male", "email"=>"myemail", "timezone"=>5, "locale"=>"en_US", "verified"=>true, "updated_time"=>"2012-07-29T06:52:12+0000"}

Solution

  • It sounds like you may be just accessing the hash incorrectly, try this:

      def self.oath(access_token, userdata)
        create! userdata.merge({:access_token => access_token})
      end