Friendly_id无效,重定向到具有错误索引的类别

时间:2012-01-30 01:27:40

标签: ruby-on-rails ruby-on-rails-3.1 friendly-id

我已经关注了Ryan Bates的截屏视频,以便为我的分类模型设置friendly_id(直到def should_generate_new_friendly_id?部分)。其中一个类别是书籍,但由于某些原因/类别/书籍重定向到空白页面,并且日志显示服务器正在尝试获取id = 0的类别(这是错误的。)所以这就是我所做的完成:

class Category < ActiveRecord::Base
  extend FriendlyId
  friendly_id :name, use: :slugged

  validates :name, :presence => true, :uniqueness => true, :length => { :in => 3..20 }
end

我的迁移:

class AddSlugToCategories < ActiveRecord::Migration
  def change
    add_column :categories, :slug, :string
    add_index :categories, :slug
  end 
end

最后但并非最不重要的是,在rails console中:

Category.find_each(&:save)

知道问题可能是什么?

编辑:

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)

1 个答案:

答案 0 :(得分:0)

事实证明,这不仅仅是friendly_id ......(请参阅原始问题的评论)

似乎缺少类别#show action:

Category.find(params[:id])