resources :furnitures
とした時に生成されるURLは↓こんな感じになると思います。
furnitures#index | GET /furnitures |
furnitures#create | POST /furnitures |
furnitures#new | GET /furnitures/:id/edit |
furnitures#edit | GET /furnitures/:id |
furnitures#show | GET /furnitures/:id |
furnitures#update | PUT /furnitures/:id |
furnitures#destroy | DELETE /furnitures/:id |
show画面だと/furnitures/1
とかになると思うのですが、この数字(:id)を変えて、/furnitures/sofa
/furnitures/table
という感じにしたい。
公式ドキュメント
resourcesにparam
オプションをつければ可能になる。
# 例えばslugカラムがあったとしたら
resources :furnitures, param: :slug
コントローラーには普段:id
を渡すところを:slug
にすればOK
def show
@furniture = Furniture.find_by(slug: params[:slug])
end
URLはちゃんとそれっぽくなる
furnitures#index | GET /furnitures |
furnitures#new | GET /furnitures/:slug/edit |
furnitures#edit | GET /furnitures/:slug |
furnitures#show | GET /furnitures/:slug |
furnitures#update | PUT /furnitures/:slug |
furnitures#destroy | DELETE /furnitures/:slug |