我使用omniauth设计用于登录Facebook用户.我希望在登录之前登录到他们所在的页面后重定向它们.
我用过了
def after_sign_in_path_for(resource_or_scope)
store_location = session[:return_to]
clear_stored_location
(store_location.nil?) ? "/" : store_location.to_s
end
Run Code Online (Sandbox Code Playgroud)
在我的应用程序控制器中,并使用此代码创建了sessions_helper
def deny_access
store_location
redirect_to new_user_session_path
end
def anyone_signed_in?
!current_user.nil?
end
private
def store_location
session[:return_to] = request.fullpath
end
def clear_stored_location
session[:return_to] = nil
end
Run Code Online (Sandbox Code Playgroud)
并解决问题,重定向到"服务/",我有Facebook和其他平台认证的逻辑,我已经使用了
skip_before_filter :store_location
Run Code Online (Sandbox Code Playgroud)
在服务和其他控制器,我不会存储为位置.
Q1我现在遇到的问题是当我使用ajax并在模态窗口中呈现登录表单时,当用户成功登录时,它会被重定向到/ users/sign_in /.我没有用户控制器,并试图创建一个sessions_controller.rb并添加了skip_before ...但它不起作用.
这是我的sign_in路线
new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
user_session POST /users/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session GET /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
Run Code Online (Sandbox Code Playgroud)
Q2当用户退出时,我尝试使用重定向
def after_sign_out_path_for(resource_or_scope)
(session[:return_to].nil?) ? "/" : session[:return_to].to_s
end
Run Code Online (Sandbox Code Playgroud)
但这只会将我重定向到根页面.
我非常感谢任何帮助,
这肯定让我困惑了几个小时.我已经通过Baugues详细介绍了我的应用程序,以便通过OAuth2进行身份验证,并且我只是在session#create
(回调)操作中测试.这是一些代码:
class SessionsController < ApplicationController
def create
@auth = request.env["omniauth.auth"]
@token = @auth["credentials"]["token"]
client = Google::APIClient.new
client.authorization.access_token = @token
service = client.discovered_api('drive', 'v1')
file_content = Google::APIClient::UploadIO.new("foo", "text/plain")
# @result = client.execute(
# :api_method => service.files.get,
# :parameters => { 'id' => 1 },
# :headers => {'Content-Type' => 'application/json'})
end
end
Run Code Online (Sandbox Code Playgroud)
在验证时,在该callback
方法中执行上述逻辑- 为了该粗略测试的目的,将其呈现出来create.html.erb
.我已经注释掉了@result
刚刚回显到视图中的实例变量.
但是,当它显然不应该Google::APIClient::UploadIO.new("foo", "text/plain")
触发uninitialized constant Google::APIClient::UploadIO
时.我已经通过这种宝石的来源挖和UploadIO
类是required
在media.rb
创业板.
建议和帮助表示赞赏!