Dashboard load_and_authorize_resource #367

Closed
htowens opened this Issue May 12, 2011 · 11 comments

Comments

Projects
None yet
4 participants

htowens commented May 12, 2011

Hi All,

I currently have a dashboard controller, from which I would like to be able to access various different models. Is there a way to do load_and_authorize_resource on various models at once?

For example, I have a model, controller and view for bags - in the bags controller, load_and_authorize_resource loads up the @bags scope, and checks authorization. How can I load the @bags scope in my dashboard controller? All permutations of load_and_authorize_resource that I have tried so far have led to @bags being returned as nil (e.g. load_and_authorize_resource :class => "Bags").

I would also want to load, for example, @rounds - would this require a second call of load_and_authorize_resource, or can it accept multiple models?

Thanks for any advice.

htowens commented May 12, 2011

This does not appear to work - as mentioned in the question, I have tried various permutations of this code, including

class DashboardController < ApplicationController
  load_and_authorize_resource :class => "Bags"
end

and

class DashboardController < ApplicationController
  load_and_authorize_resource :class => "AppName::Bags"
end

The example you have highlighted appears to be referring to loading resources in a namespaced model?

Thanks for your help.

htowens commented May 12, 2011

Not sure why et removed his post, but to clarify, if I do the following:

class DashboardController < ApplicationController
load_and_authorize_resource :class => Bag

It appears to be loading @bags, but it is nil. I get the error:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.count
Extracted source (around line #3):

1: <% title "Dashboard" %>
2: 
3: Number of Bags: <%= @bags.count %>
4: 
5: <p><%= link_to "New Bag", new_bag_path %></p>

When viewed in the Bags view, it shows the bags correctly.

Owner

ryanb commented May 12, 2011

Try load_and_authorize_resource :bag. Also, what's the name of the Dashboard's controller action?

I'll mark this for improved documentation.

htowens commented May 12, 2011

Ryan,

I have tried with the syntax you have suggested, but it still seems to be returning nil. I tried changing the syntax in my bag controller to load_and_authorize_resource :bag (it was most likely doing that anyway), and it still worked fine, so I believe the syntax is correct - there must be something else at play here.

The Dashboard's controller action is index (that is the only action in the controller) - could this be causing a conflict? The full dashboard controller is:

class DashboardController < ApplicationController
  before_filter :authenticate_user!
  load_and_authorize_resource :bag

  def index

  end

end
Owner

ryanb commented May 12, 2011

Oops, the parent parameter needs to be set too.

load_and_authorize_resource :bag, :parent => false

This is because CanCan assumes it's a parent resource if the controller name doesn't match.

htowens commented May 12, 2011

Awesome, that's done the trick - docs could definitely do with including this more prominently, I couldn't find it anywhere and I can't imagine it's that unusual a requirement.

Thanks for your help!

@htowens htowens closed this May 12, 2011

Owner

ryanb commented May 12, 2011

Reopening this issue since docs need updating.

@ryanb ryanb reopened this May 12, 2011

htowens commented May 13, 2011

It would be interesting to know the correct syntax for loading multiple models - while I can do

load_and_authorize_resource :bag, :parent => false
load_and_authorize_resource :round, :parent => false

and it works fine, it seems rather wet for a dashboard that could be loading a lot of models. I tried syntax similar to what can be used in ability.rb for authorizing multiple models (and various other permutations), but they returned errors.

I was experiencing similar confusion. Same situation where the controller had a different name than the resource and the documentation led me to try:

 load_and_authorize_resource :class => Document

which didn't work but when I switched to:

 load_and_authorize_resource :document, :parent => false

it worked perfectly... (Using cancan (1.6.7) )

I have appended the wiki to clarify this a bit (under load_resource/custom_class)

@derekprior derekprior closed this May 11, 2012

Collaborator

derekprior commented May 11, 2012

Closing. This was a doc bug and @matthewrobertson seems to have covered it.

yan13to pushed a commit to paupauorg/cancan that referenced this issue May 30, 2017

rafasoares pushed a commit to rafasoares/cancancan-wiki that referenced this issue Aug 2, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment