由于我在操作前已登录检查,因此在删除对象时我无法使用redirect_back
。
哪一个是将当前url
存储在某处的最佳方式,可以在session helper
中,然后在任何控制器中重定向到该值?
请帮忙,这个问题影响了我所有相关的控制器。
重定向向后发送给我编辑/显示,现在是零。
我目前正在删除父母:
module SessionsHelper
# Logs in the given user.
def log_in(user)
session[:user_id] = user.id
end
# Remembers a user in a persistent session.
def remember(user)
user.remember
cookies.permanent.signed[:user_id] = user.id
cookies.permanent[:remember_token] = user.remember_token
end
# Returns the current logged-in user (if any).
def current_user
@current_user ||= User.find_by(id: session[:user_id])
end
# Returns true if the user is logged in, false otherwise.
def logged_in?
!current_user.nil?
end
# Forgets a persistent session.
def forget(user)
user.forget
cookies.delete(:user_id)
cookies.delete(:remember_token)
end
# Logs out the current user.
def log_out
forget(current_user)
session.delete(:user_id)
@logs = Log.create(user_id: current_user.id, role_id: current_user.role_id, action:"Logoff")
@current_user = nil
end
# Redirects to stored Location (or to the default)
def redirect_back_or(default)
redirect_to(session[:forwarding_url] || default)
session.delete(:forwarding_url)
end
def store_location
session[:forwarding_url] = request.url if request.get?
end
end
class AddressesController < ApplicationController
before_action :logged_in_user
def address_params
params.require(:address).permit(:id, :no, :street, :suburb, :code, :details, :city_id, :addressable_type, :addressable_id)
end
def new
if Accessrule.find_by(role_id: current_user.role_id, workspace:3).try(:canwrite?)
@logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"New")
@addresses = Address.new(:addressable_type => params[:addressable_type],:addressable_id => params[:addressable_id])
else
@logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"New !Access Denied! ")
flash[:notice] = "You don't have access to create Addresses."
redirect_to :back
end
end
def create
if Accessrule.find_by(role_id: current_user.role_id, workspace:3).try(:canwrite?)
@logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"Create", details: address_params)
@address = Address.new(address_params)
if @address.save
flash[:notice] = 'Address Saved'
redirect_to @address.addressable
else
render "new"
end
else
@logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"Create !Access Denied! ")
flash[:notice] = "You don't have access to create Addresses."
redirect_to :back
end
end
def edit
if Accessrule.find_by(role_id: current_user.role_id, workspace:3).try(:canupdate?)
@logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"Edit", details: params[:id])
@addresss = Address.find(params[:id])
else
@logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"Edit !Access Denied! ", details: params[:id])
flash[:notice] = "You don't have access to edit Addresses."
redirect_to :back
end
end
def update
if Accessrule.find_by(role_id: current_user.role_id, workspace:3).try(:canupdate?)
@logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"Update", details: address_params)
@addresss = Address.find(params[:id])
if @addresss.update_attributes(address_params)
redirect_to @address.addressable
flash[:notice] = 'Account Updated'
else
render "edit"
flash[:error]
end
else
flash[:notice] = "You don't have access to edit Addresss."
redirect_to back
end
end
def show
if Accessrule.find_by(role_id: current_user.role_id, workspace:3).try(:canread?)
@logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"View", details: params[:id])
@addresses = Address.find(params[:id])
else
@logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"View !Access Denied! ", details: params[:id])
flash[:notice] = "You don't have access to view Addresss."
redirect_to back
end
end
def destroy
if Accessrule.find_by(role_id: current_user.role_id, workspace:3).try(:candelete?)
@address = Address.find(params[:id])
@logs = Log.create(user_id: current_user.id , role_id: current_user.role_id, workspace_id:3, action:"Delete", details: params[:id])
@address.destroy
redirect_to :back
flash[:notice] = 'Address Deleted'
else
flash[:notice] = "You don't have access to delete Addresss."
redirect_to :back
end
end
# Before Filters
# Confirms if User is logged-in
def logged_in_user
unless logged_in?
flash[:danger] = "Please log in."
redirect_to root_path
end
end
end
答案 0 :(得分:4)
您可以在ApplicationController中存储多个redirect_to路径...最好使用单独的before_action来执行此操作
before_action :store_back_paths
def store_back_paths
# if session[:back_path] doesn't exist, create it as an empty array
session[:back_path] ||= []
# add the current path as a new entry in the array
session[:back_path] << request.referer
# while there are more than five entries, drop the oldest entries
# this is to ensure we're not storing too many entries
session[:back_path].shift while session[:back_path].count > 5
end
执行常规重定向时,使用pop删除数组中的最后一个元素并返回删除的值。
redirect_to session[:back_path].pop
在销毁后执行重定向时,删除最后一个条目(指向已销毁项目的显示页面),然后重定向到之前的路径。
session[:back_path].pop # drops the last entry
redirect_to session[:back_path].pop
答案 1 :(得分:0)
我只是在我提交删除之前存储我想去的位置:
def destroy
@child = Child.find_by params[:id]
parent = @child.parent # or whatever
@child.destroy
flash[:notice] = 'Deleted'
redirect_to parent
end
如果你想存储在一个我不用于破坏的会话中,而是用于其他事情:
def destroy
@child = Child.find_by params[:id]
session[:return_link] = parent_path(@child.parent) # or whatever
@child.destroy
flash[:notice] = 'Deleted'
redirect_to session[:return_link]
end
当然,您可以从任何一点设置并返回,我使用它来允许用户导航网站的子部分,然后退出,通过在对子部分执行show动作时记录活动引用者,以及会话路径的显示按钮确实在他们在任何时候加入的出口处离开。
你必须为它设置添加一些测试,如果你没有设置它,当然你应该使用链接。