Вопрос

I defined a preview action as

resources :organisations, shallow: true do
  resources :orders do
    get :preview, on: :new
  end
end

In my controller I have

load_and_authorize_resource :organisation
load_and_authorize_resource :order, through: :organisation, shallow: true

My problem is not a matter of permissions, but the @order in the preview action is simply not loaded (it is nil). Is this a problem of the "on: new" not taken care of by CanCan? or is there something else escaping from my attention?

Это было полезно?

Решение

Diving into the CanCan source code, I found an option "new", that allows to do the following:

load_and_authorize_resource :order, through: :organisation, shallow: true, new: :preview

After adding this extra option, it "recognizes" the preview action as something similar to "new" and "create", and fills in the @order variable as expected...

Haven't found any documentation about this option though...

Другие советы

As far as I know, Cancan will not load your @order (It only takes care of permissions), you need to that inside your controller action.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top