问题
I've been successfully using friendly_id with multiple models in my Rails 4 app. I previously had two models that I've decided to combine (Terminals and Locations). The Terminals show action was previously working with friendly_id, where I could visit /terminals/albany and get the Terminal with the slug 'albany'.
I've gone through the bulk of the refactor to condense down to just Locations, and am on the last piece (fixing views). Along the way I've been testing things in Rails Console and using rspec, and things seem to work as expected. There are other models that still work as expected as well.
Now that I'm working on the new show page for Locations, I'm getting the following error when attempting to visit /locations/albany:
ActiveRecord::RecordNotFound in LocationsController#show
Couldn't find Location with id=albany
However, when I visit /locations/5, it successfully loads the record. It also works in Rails Console:
Location.find(5) # returns the correct location object
Location.find(5).slug # returns 'albany'
Location.friendly.find('albany') # returns the correct location object
When I look at the generated slugs in Rails dbconsole, they all look like they were generated correctly and there are no duplicates.
Here's my model:
Class Location < ActiveRecord::Base
extend FriendlyId
friendly_id :slug_candidates, use: [:slugged, :history]
def slug_candidates
[
:name,
[:name, :zip],
[:name, :zip, :location_type],
]
end
end
And here's my show action in my controller:
def show
@locations = Location.friendly.find(params[:id])
end
I'm stumped! Any hints?
回答1:
have you tried adding :finders?
friendly_id :slug_candidates, use: [:slugged, :history, :finders]
来源:https://stackoverflow.com/questions/19732047/troubleshooting-friendly-id-w-rails-4-working-as-expected-in-rails-console-ge