在我的應用程序中使用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