AbNadi
AbNadi

Reputation: 69

How to update an existing Users record

I'm working on a application which contains the soundcloud api.

i have the user login and model and i would like to append the soundcloud_id and token to the existing user but i cannot somehow update the users record. what do i do wrong?

soundcloud controller

   class SoundcloudController < ApplicationController


      def connect
      # create client object with app credentials
      client = Soundcloud.new(:client_id => ENV["SOUNDCLOUD_CLIENT_ID"],
                              :client_secret => ENV["SOUNDCLOUD_CLIENT_SECRET"],
                              :redirect_uri => "http://localhost:3000/soundcloud/oauth-callback",
                              :response_type => 'code')

      # redirect user to authorize URL
      redirect_to client.authorize_url(:grant_type => 'authorization_code', :scope => 'non-expiring', :display => 'popup')
      end

      def connected
        # create client object with app credentials
        client = Soundcloud.new(:client_id => ENV["SOUNDCLOUD_CLIENT_ID"],
                  :client_secret => ENV["SOUNDCLOUD_CLIENT_SECRET"],
                  :redirect_uri => "http://localhost:3000/soundcloud/oauth-callback")

        # exchange authorization code for access token
        access_token = client.exchange_token(:code => params[:code])
        client = Soundcloud.new(:access_token => access_token["access_token"])

        # make an authenticated call
        soundcloud_user = client.get('/me')
        unless User.where(:soundcloud_user_id => soundcloud_user["id"]).present?
        #User.create_from_soundcloud(soundcloud_user, access_token)
        UsersController.add_soundcloud_account(soundcloud_user, access_token)
        end
        sign_in_user = User.where(:soundcloud_user_id => soundcloud_user["id"])

        #create user sessions
        #session[:user_id] = sign_in_user.first.id
        redirect_to root_url, notice: "Signed in!"
      end

      def destroy
      end
    end

user controller

    class UsersController < ApplicationController


      def new
        @user = User.new
      end

      #create a user and redirect to home
      def create
        @user = User.new(user_params)
        if @user.save
          session[:user_id] = @user.id
          redirect_to '/'
        else
          redirect_to '/signup'
        end
      end


      def self.add_soundcloud_account(soundcloud_user, access_token)
        @current_user ||= User.find(session[:user_id])
        @current_user.soundcloud_user_id = soundcloud_user["id"]
        @current_user.soundcloud_access_token = access_token["access_token"]
      end

      private
      def user_params
        params.require(:user).permit(:first_name, :last_name, :email, :password)
      end
    end

Upvotes: 0

Views: 55

Answers (1)

danielrsmith
danielrsmith

Reputation: 4060

You need to call save on the @current_user and pass in the session information to the method:

def self.add_soundcloud_account(user_id, soundcloud_user, access_token)
  @current_user ||= User.find(user_id)
  @current_user.soundcloud_user_id = soundcloud_user["id"]
  @current_user.soundcloud_access_token = access_token["access_token"]
  @current_user.save
end

It is called like this:

UsersController.add_soundcloud_account(session[:user_id], soundcloud_user, access_token)

However I am not sure the add_soundcloud_account method belongs in a controller. I would prefer to see it in a Service or maybe just in the User model.

Upvotes: 1

Related Questions