Rails自动登录用户无需登录页面

时间:2013-11-20 09:20:17

标签: ruby-on-rails

我有一个控制器方法,被称为像这样的返回URL

....../paypal/success 

看起来像这样,

 def success   
            transaction = Transaction.find_by_token(params[:token])
            transaction.payer_id = params[:payer_id] 
            transaction.save
            @current_user = user = User.find(transaction.user_id)   
            username = "xxxxxxxxxxxxxxxxxxxx"
            password = "7xxxxxxxxxxxxxxx"
            signature = "xxxxxxxxxxxxxxx"       
            version = 98
            @amount = Currency.convert(user.currency, "USD", transaction.amount.to_f).to_f.round(2)
            url = "https://api-3t.paypal.com/nvp?USER=#{username}&PWD=#{password}&SIGNATURE=#{signature}&VERSION=#{version}&METHOD=DoExpressCheckoutPayment&TOKEN=#{transaction.token}&PAYERID=#{transaction.payer_id}&PAYMENTREQUEST_0_PAYMENTACTION=SALE&PAYMENTREQUEST_0_AMT=#{@amount}&PAYMENTREQUEST_0_CURRENCYCODE=USD"       
            uri = URI.parse(url)
            http = Net::HTTP.new(uri.host, uri.port)
            http.use_ssl = true
            http.verify_mode = OpenSSL::SSL::VERIFY_NONE 
            request = Net::HTTP::Get.new(uri.request_uri)
            res = http.request(request)
            response = res.body
            transaction.save
            user.balance = (user.balance+transaction.amount)
            user.save()         
            transaction.status ="Completed"
            transaction.save()  
            flash[:notice] = 'Payment Transaction Completed. Thank you for using skyline SMS'
            redirect_to user_home_path(@current_user)     

然而,行

redirect_to user_home_path(@current_user)

取而代之的是用户登录页面。

如何从该方法登录用户,以便将其重定向到其主页。

认证系统正在设计中 提前谢谢。

1 个答案:

答案 0 :(得分:0)

您可以使用Devise::Controllers::Helpers中已经混合到ApplicationController中的sign_in()方法,因此可供所有控制器使用。因此,在您的成功行动中,您将执行以下操作:

user = User.find(transaction.user_id) 
sign_in(user)

Devise :: Controllers :: Helpers又包含Devise::Controllers::SignInOut,您可以在其中看到实际代码。