2
Cancanを取得しようとすると、アプリケーションでいくつかのモデルを保護し、それが私が思ったように動作しない理由が不思議に思う。私は、この例ではなく、クラス全体とは対照的に特定のインスタンスに対してcan?
を実行できると思っていましたが、ポストのリストが表示されるので、インスタンスごとに能力を有効にできますか?Cancanの能力を理解する
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
あなたのビューコードも同様です。 –