Routing in rails3 with user primary id

I have a model with a user primary key:

document.rb

class Document < ActiveRecord::Base
  set_primary_key "token"
end

routes.rb:

MyApp::Application.routes.draw do
  resources :documents, :only => [:index, :show, :create]
end

When I create a new document, I get an error message:

No route matches {:controller=>"documents", :id=>#<Document id: "b430cfe73aaa5235fbfe", token: "b430cfe73aaa...

When I switch to using: id as the primary key, everything is fine. But I need to use a token.

I use: rails 3.0.0 and ruby ​​1.8.7 (2010-04-19 patchlevel 253) [i686-linux], MBARI 0x8770, Ruby Enterprise Edition 2010.02

Thanks for the help.

+3
source share
1 answer

try adding to document.rb

def to_param
 token
end
+3
source

Source: https://habr.com/ru/post/1765985/


All Articles