在我的应用程序中使用CanCan进行授权。我目前已将每个用户的所有信息存储在/ users/2或users/3或/ users/4等等。CanCan的用户授权:NoMethodError
我的问题是,当用户登录时,他们可以修改URL并查看其他用户的敏感内容。我如何使用CanCan防止用户(比如说用户/ 2)看到(用户/ 3)?
当我 “load_and_authorize_resource” 添加到我的用户控制器我收到以下错误:
NoMethodError in UsersController#show
undefined method `user_id' for #<User:0x007fee61e90b90>
模型
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.has_role? :admin
can :manage, :all
can :access, :rails_admin
can :dashboard
else
can :manage, User, :user_id => user.id
end
控制器
class ApplicationController < ActionController::Base
protect_from_forgery
rescue_from CanCan::AccessDenied do |exception|
redirect_to root_path, :alert => exception.message
end
class UsersController < ApplicationController
before_filter :authenticate_user!
load_and_authorize_resource
def show
@user = User.find(params[:id])
@date = params[:month] ? Date.parse("#{params[:month]}-01") : Date.today
@occasions = @user.occasions.page(params[:page]).per(5)
end
给了我一个“未定义的方法user_id”在users_controller =>显示... – js111
你能否也请添加你的视图代码? – Pierre
想通了..只需要改变“可以:管理,用户,:user_id ==用户”以小写ü“可以:管理,用户,:user_id ==用户” – js111