简体   繁体   English

在Rails中,使用omniauth成功登录后,如何重定向回原始页面?

[英]In Rails, how do I redirect back to the original page after login success with omniauth?

I'm using Rails 4.2.7 and omniauth. 我正在使用Rails 4.2.7和omniauth。 I have this partial included on a couple of my pages 我的部分页面中包含了部分内容

    <div id="loginLogos">
            <%= link_to image_tag("google_plus_icon.jpg", :border => 0, :alt => 'Google'), '/auth/google' %>
            <%= link_to image_tag("facebook.png", :border => 0, :alt => 'Facebook'), '/auth/facebook' %>
            <%= link_to image_tag("twitter_icon.png", :border => 0, :alt => 'Twitter'), '/auth/twitter' %>
            <%= link_to image_tag("linkedin.png", :border => 0, :alt => 'LinkedIn'), '/auth/linkedin' %>
    </div>

After someone successfully signs in, I would like them to be redirected back to the page that spawned the above partial. 某人成功登录后,我希望将他们重定向回产生上述部分内容的页面。 I read in another answer that I need to have this method 我读了另一个答案,我需要这种方法

def store_return_to
  session[:return_to] = request.url
end

but the answer does not specify where to put it. 但答案并未指定放置位置。 Do I need to include it in my application controller or every controller that serves a view that renders the partial I reference? 我是否需要将其包含在我的应用程序控制器中,或者是否需要提供呈现部分我引用的视图的每个控制器?

be sure to specify your callback in initializer. 确保在初始化程序中指定您的回调。 for example for facebook: 例如对于facebook:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, "app_id", "app_secret",
    scope: 'public_profile,email,manage_pages,pages_show_list', info_fields: 'id,name,link', display: 'popup', callback_path: '/stores/facebook_callback'

end

and don't forget to add it to the routes ;) 并且不要忘记将其添加到路线中;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM