简体   繁体   English

Rails:登录后无法重定向到上一页

[英]Rails: redirect to previous page after login doesn't work

I am trying to redirect to the page from where I clicked login, but after logining in it doesn't redierect to previous page but stays on login page (although the user is already logged in). 我试图重定向到单击登录的页面,但是登录后不会重定向到上一页,而是停留在登录页面上(尽管用户已经登录)。 Here is my code: 这是我的代码:

session_helper.rb session_helper.rb

module SessionsHelper

def sign_in(user)
    remember_token = User.new_remember_token
    cookies.permanent[:remember_token] = remember_token
    user.update_attribute(:remember_token, User.encrypt(remember_token))
    self.current_user = user
end


def redirect_back_or(default)
    redirect_to(session[:return_to] || default)
    session.delete(:return_to)
end

def store_location
    session[:return_to] = request.fullpath
end
end

sessions_controller.rb sessions_controller.rb

class SessionsController < ApplicationController
  include SessionsHelper
  def new
  end
  def create
    user = User.find_by_username(params[:session][:username])
    if user && user.authenticate(params[:session][:password])   
      cookies.permanent[:remember_token] = user.remember_token
      #redirect_to root_url,:notice => "Logged in!"
        redirect_back_or user
    else
      flash[:error] = 'Invalid email/password combination' # Not quite right!
      render 'new'
    end
  end
  def destroy
    cookies.delete(:remember_token)
    #session[:user_id] = nil
    redirect_to root_url, :notice => "Logged out!"
  end
end

I also tried to write in create function in sessions_controller.rb 我也试图在sessions_controller.rb中写create函数

redirect_to request.referer redirect_to request.referer

but it doesn't work. 但这不起作用。

Am I missing something? 我想念什么吗?

Thanks for your help! 谢谢你的帮助!

The problem happens at store_location . 问题发生在store_location

Though you havn't said in question, I guess you probably put this method in before_filter . 尽管您没有说过问题,但我想您可能将此方法放在了before_filter So, no matter GET or POST or other request, the request hit this filter at first and store location. 因此,无论是GET还是POST或其他请求,该请求都首先到达此过滤器并存储位置。

Now, in this case, actually the user has two requests. 现在,在这种情况下,实际上用户有两个请求。 One is to #new by GET, and the other is to #create by POST. 一种是通过GET #new ,另一种是通过POST #create In the later, his last request to #new was recorded as the going back location. 后来,他对#new最后一个请求被记录为返回位置。 So you'll see you always go back to #new :) 所以您会看到您总是回到#new :)

The solution is to filter the location to be stored. 解决方案是过滤要存储的位置。

def store_location
  disable_pattern = /\A\/user*/
  session[:return_to] = request.fullpath unless request.fullpath ~= disable_pattern
end

This pattern could solve current problem but not exclusive. 这种模式可以解决当前的问题,但不是排他的。 In practice you may see even JS/JSON requests has been recorded, so you may need to add more restrictions according to the specific case. 实际上,您甚至可能已经记录了JS / JSON请求,因此您可能需要根据具体情况添加更多限制。 For example, only apply before_filter on #show or #index, use white list, etc. 例如,仅在#show或#index上应用before_filter ,使用白名单等。

I think request.referer may not have worked because of a typo in the method. 我认为request.referer可能由于该方法中的输入错误而无法正常工作。 It should be request.referrer . 应该是request.referrer

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

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