如果我有一个像下面这样的控制器,那么保护我的表演动作的最佳方法是什么?当前设置的方式是,任何用户都可以仅使用ID来查看其他用户的注释。
class NotesController < ApplicationController
before_action :authenticate_user!
def index
@notes = Note.where(user_id: current_user)
end
def show
@note = Note.find(params[:id])
end
def new
@note = current_user.notes.build
end
end
方法:1
def show
@note = Note.find(params[:id])
redirect_to anywhere_you_want, notice: "can't see others note" if @note.user != current_user
end
方法:2
创建一个before_action
像:
before_action :authenticate_note_user, only: [:show]
private
def authenticate_note_user
@note = Note.find(params[:id])
redirect_to anywhere_you_want, notice: "can't see others note" if @note.user != current_user
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句