Ruby on rails 设计前动作不起作用,允许旁路
我遇到了一个问题。我试图通过如下所示设置“正确的用户”方法,并在顶部设置Desive的before action,不让用户查看其他帖子、线程(PIN)。然而,当我以用户身份登录并试图绕过它时,它会工作,我可以编辑、销毁。它不是重定向到pins_路径 甚至没有错误发生,所以真的很难解释到底发生了什么。我将发布控制器的代码,请询问您是否还需要查看。谢谢Ruby on rails 设计前动作不起作用,允许旁路,ruby-on-rails,ruby,Ruby On Rails,Ruby,我遇到了一个问题。我试图通过如下所示设置“正确的用户”方法,并在顶部设置Desive的before action,不让用户查看其他帖子、线程(PIN)。然而,当我以用户身份登录并试图绕过它时,它会工作,我可以编辑、销毁。它不是重定向到pins_路径 甚至没有错误发生,所以真的很难解释到底发生了什么。我将发布控制器的代码,请询问您是否还需要查看。谢谢 class PinsController < ApplicationController before_action :set_pin,
class PinsController < ApplicationController
before_action :set_pin, only: [:show, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
@pins = Pin.all
end
def show
end
def new
@pin = current_user.pins.build
end
def edit
end
def create
@pin = Pin.new(pin_params)
if @pin.save
redirect_to @pin, notice: 'Pin was successfully created.'
else
render action: 'new'
end
end
def update
if @pin.update(pin_params)
redirect_to @pin, notice: 'Pin was successfully updated.'
else
render action: 'edit'
end
end
def destroy
@pin.destroy
redirect_to pins_url
end
private
# Use callbacks to share common setup or constraints between actions.
def set_pin
@pin = Pin.find(params[:id])
end
def correct_user
@pin = current_user.pins.find_by(id: params[:id])
redirect_to pins_path, notice: "Not allowed!" if @pin.nil?
end
def pin_params
params.require(:pin).permit(:description)
end
end
class PinsController
为什么您没有执行当前用户.pins.find(params[:id])
?我尝试过,但它仍然在绕过。您正在检查用户是否正确,甚至在对用户进行身份验证之前。。。将correct\u user
放在authenticate\u user
之后。。。这里可能有不止一个bug,但您至少需要这样做(因为否则,当前用户
将为空):(您可能应该将验证用户
作为第一个过滤器)它看起来也像是用户的pin。您可以检查@pin.user==current\u user
或pin.user\u id==current\u user.id
。通过这种方式,您可以只获取之前在另一个设置中设置的@pin,而不是查询另一个select。为什么您没有执行当前用户.pins.find(params[:id])
?我尝试过,但它仍在绕过。您正在检查用户是否正确,甚至在验证用户之前。。。将correct\u user
放在authenticate\u user
之后。。。这里可能有不止一个bug,但您至少需要这样做(因为否则,当前用户
将为空):(您可能应该将验证用户
作为第一个过滤器)它看起来也像是用户的pin。您可以检查@pin.user==current\u user
或pin.user\u id==current\u user.id
。这样,您只需获取之前在另一个选择中设置的@pin,而无需查询另一个选择。