简体   繁体   中英

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. 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:

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 ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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