0

I'm new to rails and currently involved in an internship and I was assigned to use the friendly_id gem for my tournament class, this is part of the code in it:

class Tournament < ApplicationRecord
  extend FriendlyId
  friendly_id :url_id
  ...
end

I don't use a slug since I have a url_id attribute that stores my desired url and when I try with the old .../tournaments/1 everything's all good but with .../tournaments/example I get "example is not a valid value for id" with code 103, status 400. Any ideas what the problem might be?

Robert
  • 178
  • 1
  • 8

1 Answers1

2

You have to update your controller for Tournaments so that it uses friendly.find method instead of the find.

# Change this:
Tournament.find(params[:id])

# to
Tournament.friendly.find(params[:id])
Uzbekjon
  • 11,655
  • 3
  • 37
  • 54
  • The `tournaments_controller.rb` file is empty – Robert Aug 09 '16 at 08:43
  • Well, look into your logs and find the exact line where that `example is not a valid value for id` error is being raised and you'll find your file. – Uzbekjon Aug 09 '16 at 08:45
  • Started GET "/api/tournaments/example" for ::1 at 2016-08-09 11:48:27 +0300 [1m[36mActiveRecord::SchemaMigration Load (1.6ms)[0m [1m[34mSELECT "schema_migrations".* FROM "schema_migrations"[0m Processing by TournamentsController#show as API_JSON Parameters: {"id"=>"example"} Completed 400 Bad Request in 7ms (Views: 0.3ms | ActiveRecord: 0.0ms) I don't get it, is it handled by `tournaments_controller.rb` ? – Robert Aug 09 '16 at 08:51
  • That `example is not a valid value for id` message I got from using the open http-requester pluggin in firefox. – Robert Aug 09 '16 at 08:54
  • Apparently your `tournaments_controller.rb` file has something. At the very least it got to have `respond_with` call. Paste your entire file contents here please... – Uzbekjon Aug 09 '16 at 10:29
  • All it has is `class TournamentsController < ApplicationController end` so does each controller file for the other models, I don't understand, is there some kind of workaround to allow such empty controllers, because I can't seem to find anything. – Robert Aug 09 '16 at 10:44
  • Also when I added a show method in the tournament controller and pasted your code plus the json rendering, I managed to make it working but my tutor said that's not how it should be done. – Robert Aug 09 '16 at 10:46
  • From what I understand, we're using the gem jsonapi-resources and we use its method show, `class ApplicationController < ActionController::API include JSONAPI::ActsAsResourceController end` – Robert Aug 09 '16 at 12:05