0
나는 soundcloud API가 포함 된 응용 프로그램에서 작업하고 있습니다.기존 사용자 레코드를 업데이트하는 방법
나는 사용자 로그인 및 모델을 가지고 있으며 기존 사용자에게 soundcloud_id 및 토큰을 추가하고 싶지만 어떻게 든 사용자 레코드를 업데이트 할 수 없습니다. 내가 뭘 잘못 했니?
사운드 클라우드 컨트롤러
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
사용자 컨트롤러
당신은@current_user
에
save
를 호출하는 방법에 세션 정보를 전달해야
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
나는 그 메소드에 도달 할 때마다 eror를 얻습니다 : "정의되지 않은 지역 변수 또는 메소드 'Session'for UsersController : Class" – AbNadi
이 업데이트되었습니다. 일부 리팩토링 생각을 추가했습니다. – danielrsmith
그것은 작동합니다, 고마워요. – AbNadi