Friendly_id not working, redirects to Category with wrong index
-
04-12-2019 - |
質問
I've followed Ryan Bates' screencast in order to get friendly_id set up (up until the def should_generate_new_friendly_id? part) for my Category model. One of the Categories is books, but for some reason /categories/books redirects to a blank page, and the log shows that the server is trying to fetch a category with id=0 (which is wrong.) So here's what I've done:
class Category < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :slugged
validates :name, :presence => true, :uniqueness => true, :length => { :in => 3..20 }
end
My migration:
class AddSlugToCategories < ActiveRecord::Migration
def change
add_column :categories, :slug, :string
add_index :categories, :slug
end
end
And last but not least, in rails console:
Category.find_each(&:save)
Any idea what the problem could be?
Edit:
Started GET "/categories/books" for 127.0.0.1 at 2012-03-02 13:03:29 -0500
Processing by CategoriesController#show as HTML
Parameters: {"id"=>"books"}
Category Load (0.4ms) SELECT "categories".* FROM "categories"
Item Load (0.6ms) SELECT "items".* FROM "items" WHERE "items"."category_id" = 0
Rendered categories/_items.html.erb (1.9ms)
Rendered categories/show.html.erb within layouts/application (20.7ms)
Completed 200 OK in 139ms (Views: 69.1ms | ActiveRecord: 6.6ms)
解決
This turned out to be more than just friendly_id ... (see comments on original question)
It seems as though the category#show action is missing:
Category.find(params[:id])
所属していません StackOverflow