我已经在视图和控制器中执行了所有必要的步骤,以防止用户编辑不属于它们的表单。但是,如果用户A使用url直接访问该站点(例如:travel / 5 / trips / new),则即使旅程5属于用户B,它仍将允许用户A在旅程5下创建新旅程。
应对这种情况的最佳方式是什么?我正在使用devise 3.4
在行程控制器,你可以添加一个before_filter
方法来检查的旅程是否属于current_user
或不
before_filter :authorize_journey, only: [:new]
def authorize_journey
@journey = current_user.journeys.where(id: params[:journey_id]).first
if @journey.blank?
flash[:error] = "You cannot access someone else's record."
redirect_to root_path
end
end
希望这可以帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句