简体   繁体   中英

rails 4 gem 'mongoid_slug' unable to search by id

I just installed the gem 'mongoid_slug', here is the model:

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

In the controller I have a function call get_book that I call before edit, show etc
Of course it is not working, I also tried find_by_slug.
Error Document not found for class Book with attributes {:id=>"try-new-book"} .

Request info

Request parameters
{"action"=>"show", "controller"=>"startups", "id"=>"try-new-book"}

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

Thank you

According to docs it should works just:

Book.find params[:id]

Updated

I answered the same question here . So in short: change id.start_with?('....') to something like id =~ /^[[:alnum:]]+$/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM