是否有一种简单的方法可以编写一个帮助方法来始终更新会话中先前访问的 url。我尝试了下面的方法,但保存的 url 始终是当前的。我希望能够在我的所有控制器中使用这个帮助器进行重定向。
#application_controller.rb
class ApplicationController < ActionController::Base
before_filter :my_previous_url
def my_previous_url
session[:previous_url] = request.referrer
end
helper_method :my_previous_url
end
我在用户控制器的更新方法中使用了它,如下所示,但它总是重定向到同一个打开的 url(有点像刷新被击中)。
def update
if current_user.admin == true and @user.update(user_params)
redirect_to my_previous_url, notice: "Password for User #{@user.username} has Successfully been Changed."
return
elsif current_user.admin == false and @user.update(user_params)
session[:user_id] = nil
redirect_to login_path, notice: "Password for User #{@user.username} has Successfully been Changed. Please Log-In Using the New Password."
return
end
respond_to do |format|
if @user.update(user_params)
changed = true
format.html { redirect_to logout_path }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end