Giter Site home page Giter Site logo

Comments (3)

rklubenspies avatar rklubenspies commented on August 18, 2024

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

from slugged.

hkockerbeck avatar hkockerbeck commented on August 18, 2024

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.

from slugged.

Sutto avatar Sutto commented on August 18, 2024

@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.

from slugged.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.