rails 4 gem' mongoid_slug'无法按ID搜索

时间:2014-06-14 01:57:58

标签: ruby-on-rails ruby ruby-on-rails-4 mongoid slug

我刚刚安装了gem'mongoid_slug',这是模型:

class Book
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Slug

  field :_id, type: String, slug_id_strategy: lambda {|id| id.start_with?('....')}

  field :name, type: String
  slug :name, 
  ...
end

在控制器中我有一个函数调用get_book,我在编辑之前调用,显示等 当然它不起作用,我也尝试过find_by_slug 对于包含属性Document not found的类Book,错误{:id=>"try-new-book"}

请求信息

请求参数
{"action"=>"show", "controller"=>"startups", "id"=>"try-new-book"}

def get_book
  @book = Book.find_by(id: params[:id])
end

谢谢

1 个答案:

答案 0 :(得分:1)

According to docs它应该只是:

Book.find params[:id]

<强>更新

我回答了同样的问题here。简而言之:将id.start_with?('....')更改为id =~ /^[[:alnum:]]+$/

之类的内容