如何更新现有用户记录

时间:2016-06-25 22:20:53

标签: ruby-on-rails

我正在开发一个包含soundcloud api的应用程序。

我有用户登录和模型,我想将soundcloud_id和令牌附加到现有用户,但我无法以某种方式更新用户记录。 我做错了什么?

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

用户控制器

    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

1 个答案:

答案 0 :(得分:1)

您需要在save上调用@current_user并将会话信息传递给该方法:

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

它被称为:

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

但是我不确定add_soundcloud_account方法是否属于控制器。我更愿意在服务中看到它,或者只是在用户模型中看到它。