Rails&Devise:如何验证特定用户?

Ank*_*oni 4 authentication devise ruby-on-rails-3

我第一次使用Devise和rails,我遇到了一件事:我authenticate_user!在用户控制器中使用提供的方法来限制对这样的页面的访问: before_filter :authenticate_user!, :only => [:edit, :show, :update, :create, :destroy]

但是这允许任何已登录的用户访问任何其他用户:edit操作,我想仅限制该用户.我该怎么办?

Dex*_*Dex 5

在编辑方法中,您需要检查用户是否拥有该记录:

def edit
   @record = Record.find(params[:id])
   if @record.user == current_user
      @record.update_attributes(params[:record])
   else
      redirect_to root_path
   end
end
Run Code Online (Sandbox Code Playgroud)