您可以记住会话中的 url。这就是我在我的应用程序中实现此功能的方式。
将这些方法添加到您的 ApplicationController
def store_location location=nil
session[:return_to] = location || request.request_uri
end
def redirect_back_or_default(default)
redirect_to(session[:return_to] || default)
session[:return_to] = nil
end
def http_referrer
http_referer, request_path = request.env["HTTP_REFERER"],
request.env["REQUEST_PATH"]
return nil unless (http_referer and request_path and
http_referer =~ Regexp.new("^#{SITE_ROOT}") and
http_referer != (SITE_ROOT + request_path))
return http_referer
end
在UserController edit/new action 中存储引用者。
before_filter :store_location, :only => [:new, :edit]
def store_location
super http_referrer
end
在UserController create/update 操作中返回并重置引荐来源网址。
def create
if @user.save
flash[:notice] = "Successfully created the user."
redirect_back_or_default root_url
else
render :action => 'new'
end
end
def update
if @user.save
flash[:notice] = "Successfully created the user."
redirect_back_or_default root_url
else
render :action => 'new'
end
end