リモートで働くプログラマーの検索結果

リモ太がググったことの覚書

Railsのroutingで詳細ページのparamsをid以外に変える

結論を先に paramオプションをつける

通常 routes.rbでroutelingを記述すると

Rails.application.routes.draw do
  resources :posts
end
                                posts GET    /posts(.:format)                                                                         posts#index
                                      POST   /posts(.:format)                                                                         posts#create
                             new_post GET    /posts/new(.:format)                                                                     posts#new
                            edit_post GET    /posts/:id/edit(.:format)                                                                posts#edit
                                 post GET    /posts/:id(.:format)                                                                     posts#show
                                      PATCH  /posts/:id(.:format)                                                                     posts#update
                                      PUT    /posts/:id(.:format)                                                                     posts#update
                                      DELETE /posts/:id(.:format)                                                                     posts#destroy

paramを追加すると idの部分が paramで指定したものに変わる

Rails.application.routes.draw do
  resources :posts, param: :token
end
                                posts GET    /posts(.:format)                                                                         posts#index
                                      POST   /posts(.:format)                                                                         posts#create
                             new_post GET    /posts/new(.:format)                                                                     posts#new
                            edit_post GET    /posts/:token/edit(.:format)                                                             posts#edit
                                 post GET    /posts/:token(.:format)                                                                  posts#show
                                      PATCH  /posts/:token(.:format)                                                                  posts#update
                                      PUT    /posts/:token(.:format)                                                                  posts#update
                                      DELETE /posts/:token(.:format)                                                                  posts#destroy