Works in console, not in controller
Hi,
i am pretty sure i am doing things right, but something is off. I have this in controller:
@location = Location.friendly.find(params[:id])
Model:
extend FriendlyId
friendly_id :name, use: :slugged
And the error says:
Couldn't find Location with 'id'=my-super-name
raise RecordNotFound, "Couldn't find #{name} with '#{primary_key}'=#{id}"
Yet, in console i get record just fine like this:
Location.friendly.find('my-super-name')
Controller works when using
friendly_id :name, use: [:slugged, :finders]
But i think it should work without it, too, as i am doing a friendly.find. :finders, as expected also work without friendy.find. - but i would like to control this...
I am on Rails 4.2.1 - Ruby 2.1.2 I have already searched around, but only found this one single Stackoverflow topic without proper answer to solve this.
Thx
Did you restart the server after adding FriendlyId? Try printing
params.inspect in the log or with puts to see what the id in the
controller is, any chance there's a space?
On Fri, May 22, 2015 at 09:30 Mitja Čebokli [email protected]
wrote:
Hi,
i am pretty sure i am doing things right, but something is off. I have this in controller:
@location = Location.friendly.find(params[:id])
Model:
extend FriendlyId friendly_id :name, use: :slugged
And the error says:
Couldn't find Location with 'id'=my-super-name raise RecordNotFound, "Couldn't find #{name} with '#{primary_key}'=#{id}"
Yet, in console i get record just fine like this:
Location.friendly.find('my-super-name')
Controller works when using
friendly_id :name, use: [:slugged, :finders]
But i think it should work without it, too, as i am doing a friendly.find. :finders, as expected also work without friendy.find. - but i would like to control this...
I am on Rails 4.2.1 - Ruby 2.1.2 I have already searched around, but only found this one single Stackoverflow http://stackoverflow.com/questions/20314927/rails-friendly-id-couldnt-find-category-with-id-electronics topic without proper answer to solve this.
Thx
— Reply to this email directly or view it on GitHub https://github.com/norman/friendly_id/issues/666.
Restarted server numerous times :) Parameter comes through fine.
{"id"=>"my-super-name"}
Looking through the log i've found the difference on select itself. Finders:
Location Load (1.1ms) SELECT `locations`.* FROM `locations` WHERE `locations`.`slug` = 'my-super-name' ORDER BY `locations`.`id` ASC LIMIT 1
No finders (both time using friendly.find):
Location Load (1.0ms) SELECT `locations`.* FROM `locations` WHERE `locations`.`id` = 0 LIMIT 1
What version of FriendlyId are you using? On Fri, May 22, 2015 at 10:13 Mitja Čebokli [email protected] wrote:
Parameter comes through fine.
{"id"=>"my-super-name"}
Looking through the log i've found the difference on select itself. Finders:
Location Load (1.1ms) SELECT
locations.* FROMlocationsWHERElocations.slug= 'my-super-name' ORDER BYlocations.idASC LIMIT 1No finders (both time using friendly.find):
Location Load (1.0ms) SELECT
locations.* FROMlocationsWHERElocations.id= 0 LIMIT 1— Reply to this email directly or view it on GitHub https://github.com/norman/friendly_id/issues/666#issuecomment-104656226.
friendly_id 5.1.0
maybe try taking out finders?
friendly_id :name, use: [:slugged]
No, that's the whole point > friendly.find doesn't work without :finders. But with :finders, normal find works as friendly.find. No good.
Would love to help with this, but without having your application code in front of me there's little I can do. If you can provide a sample app that reproduce there error I'd be more than happy to look into it.
Also seeing the same issue on rails 4.2.1
Seem to have the same issues as well in 4.2.1 @mesge any luck?
Unfotunately not @nickyrasch. As a work around, I took this advice and removed finders. find now works as friendly.find.
Look... using version '5.0.3' the error was "undefined method `primary' for #ActiveRecord::ConnectionAdapters::Column:0x000000093ce2c8. I modified it "Room.friendly.find(params[:id])" for this "Room.friendly.find_by(id: params[:id])", and work.
Using version "5.1.0" the error is "raise RecordNotFound". I modified the same thing, but one functionality broken. So i recommend use version "5.0.3".
Looks like this issue is staled and can be closed. Xxx.friendly.find works good both in controller and console by default. So, this problem should be somewhere else, but not in friendly_id itself. But, please, fill free to open issue again if you find additional details to clarify problem.
This issue is closed. However, I had the same problem and the answer was that friendly doesn't seem to work with "load_and_authorize_resource".
This issue is closed. However, I had the same problem and the answer was that friendly doesn't seem to work with "load_and_authorize_resource".
YOU SAVED ME!!!