LoginSignup
6

More than 5 years have passed since last update.

Rails でリソース ID にドットを含めつつフォーマットを指定する

Posted at

地味にハマったのでメモ。

Rails でリソース ID にドットを含めたい場合、

routes.rb
resources :users, id: %r{[^/]+} do
  ...
end

と書くと、フォーマットも ID に含まれてしまう。
例えば、/users/bob.dylan.json にアクセスすると、id: bob.dylan.json, format: html なんてことになる。

これを回避するには、以下のように書けば良かった。

routes.rb
resources :users, id: %r{[^/]+?}, format: /json|csv|xml|html/ do
  ...
end

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
6