Closed rklubenspies closed 13 years ago
This issue has been fixed by doing the following:
# in controller
load_and_authorize_resource :find_by => :slug
# in model
def self.find_by_slug!(slug)
find_using_slug(slug)
end
All credit goes to ryanb and the entire thread can be seen at https://github.com/ryanb/cancan/issues#issue/256
Because I'm not sure where it fits better, I post my suggestion from the CanCan issue linked above here as well:
There's a better solution in my opinion. Rails generates dynamic finders for a models attributes. But the name of the attribute that Slugged adds isn't slug
, but cached_slug
. So with
load_and_authorize_resource :find_by => :cached_slug
we not only save the trouble of adding a custom find_by_slug!
method to each of possible many models. Additionally, if we try to find
a slug that isn't there, find_by_cached_slug!
will raise ActiveRecord::RecordNotFound as expected. The custom find_by_slug!
posted above will just deliver nil, on which the views will probably throw up later.
@hkockerbeck hence it should use find_using_slug!; The advantage of the latter is it can use the history feature; If you're not using that at all, using find_by_cached_slug is probably a better idea.
Hey there!
I'm having a little issue using Slugged at the same time as CanCan (an authorization gem). I also posted this to CanCan's GitHub issues so that its author would know about it. Basically when CanCan loads and authorizes my resource which also has a slug tied to it, CanCan places the authorization code in the before_filter. By doing this, it triggers a ActiveRecord::RecordNotFound exception. I'm then told "Couldn't find Page with ID=home" thus Slugged was never integrated for some reason... to make things odder, none of this happens when I take CanCan's authorization code out. I cannot however, completely remove CanCan from my app, since the whole thing is built on it. Do you have any suggestions on how to fix this?
Thank you for your help! It is much appreciated!
-Robert