jeudi 21 janvier 2016

How to implement authorization?

Suppose, I have a model called Animal. This model contains enum attribute kind with two possible states.

class Animal < ActiveRecord::Base
  enum kind: [ :cat, :dog ]
end

Then in my controller I create different instance variables.

class AnimalsController < ApplicationController
  def index
    @cats = Animal.cat
    @dogs = Animal.dog
  end
end

In my view I got two separate collections.

<h1>Animals</h1>

<%= render partial: 'animals/cat', collection: @cats, as: :cat %>
<%= render partial: 'animals/dog', collection: @dogs, as: :dog %>

How can I make an authorization to be able to edit the first collection's resources and not to be able to edit the second ones?

The following approach won't work because it works only for one action entirely.

before_action :current_user_only, except: [:edit]

So, how do I implement that kind of authorization?

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire