我正在通过 Hartl 的教程学习 Rails,但我可以获得第 9.2.3 章 Link去上类
本节之前的所有内容在本节之前都运行良好,但是在到达这里之后,如果我访问/users/1/edit 并使用不同或相同的用户登录,它将重定向回用户页面而不是编辑页面。
session 助手:
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
before_filter 的用户 Controller 方法
private
def signed_in_user
unless signed_in?
store_location
redirect_to signin_path, notice: "Please sign in."
end
end
def correct_user
@user = User.find(params[:id])
redirect_to(root_path) unless current_user?(@user)
end
用户 Controller 中的过滤器之前
before_filter :signed_in_user,only:[:edit,:update,:index]
before_filter :correct_user, only:[:edit,:update]
到目前为止我的代码Github
最佳答案
您错过了在SessionsController创建中调用redirect_back_or user而不是redirect_to:
def create
user = User.find_by_email(params[:session][:email])
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_back_or user
else
flash.now[:error] = 'Invalid email/password combination' # Not quite right!
render 'new'
end
end
我通过将session添加到页面底部的调试信息中发现,登录成功后return_to仍然存在:
{"session_id"=>"4dcc1baaf651aaab953026902b32f805", "_csrf_token"=>"0YkP+yd/DDuInX4kIBkjwbzxV1GK0oYBDAXEiWnHLGs=", "return_to"=>"/users/1/edit"}
关于ruby-on-rails - Rails 登录后不重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11712203/