3

私はこれらの2つのモデルを持っています:

class Photo < ActiveRecord::Base
  belongs_to :user
end

class User < ActiveRecord::Base
  has_many :photos
end

declarative_authorizationそして、役割のこの設定:

  role :reg_ser do
     has_permission_on :photos, :to => [:show] do
       if_attribute :user_id => is { user.id }
     end
  end

そして、彼がアップロードした画像をユーザーに表示できるようにしたい-彼の画像のみ。たとえば、次のようになります。

user_id | photo
      1 | 1
      1 | 2
      1 | 3
      2 | 4

そして、ユーザーが url を設定すると、この画像は数字で/photos/1のみ表示され、がこのアドレスを表示すると、画像は表示されません...user_id1user_id=2

このようなことは可能ですか?

4

1 に答える 1

0

次のようなものを置く必要があります

filter_access_to [:index, :new, :create]
filter_access_to [:show, :edit, :update], :attribute_check => true

コントローラーなどで

role :reg_ser do
  has_permission_on :photos, :to => [:index, :new, :create]
  has_permission_on :photos, :to => [:show, :edit: update] do
    if_attribute :user_id => is { user.id }
  end
end

authorization_rules.rb:attribute_checkしたがって、すべてが適切に機能するため、属性を確認するすべてのアクションのコントローラーにを含めることが重要です。:index追加することで現在のユーザーのものを表示するのは非常に簡単なので、私はそれなしで残しました

def begin_of_association_chain
  current_user
end

またはコントローラーに似ています(これにより、InheritedResourcesがcurrent_user.picturesコレクションとして保持されます)。

http://asciicasts.com/episodes/188-declarative-authorizationを読むことにも興味があるかもしれません

于 2012-03-13T21:43:57.340 に答える