2
试图让惨惨确保在应用程序和好奇的几个模型为什么它不工作,我以为它会的方式。我以为你可以在特定的实例上使用can?
而不是整个类,所以在本例中不是这样,但是可以在每个实例的基础上启用能力,因为显示的是帖子列表?了解惨惨能力
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.role? :admin
can :manage, :all
elsif user.role? :moderator
can :manage, Post
else
can :read, :all
end
end
end
# posts/index.html.haml
...
- if can? :update, @post <- doesn't work
- if can? :update, Post <- works
编辑:添加PostsController.rb
#posts_controller.rb
class PostsController < ApplicationController
before_filter :login_required, :except => [:index, :show]
load_and_authorize_resource :except => [:create]
def index
# @posts = Post.all ## <- handled by Cancan's load_and_authorize_resource
@events = Event.where("end_date <= :today", :today => Date.today)
@next_event = Event.next
respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
end
end
...
end
你可以使用第一种方法,你的控制器对于索引动作是什么样的? –
好的。添加上面......不知道'load_and_authorize_resource'是否造成麻烦......但这是Cancan的方法,所以我不会期望它... – Meltemi
而且你的视图代码也是,忘了问这个问题。 –