LoginSignup
0
1

More than 5 years have passed since last update.

Rails のルーティングで GET パラメータの中身を使ってリダイレクト先を変更

Last updated at Posted at 2016-09-26

redirect に渡すブロックの中で、 req.params にリクエストされたパラメータが入っているのでそれを使う

get :foo, to: redirect { |params, req| req.params[:bar].present? ? "/foo/#{req.params[:bar]}" : "/baz" }

こうすることで

/foo           # /baz へリダイレクト
/foo?bar=qux   # /foo/qux へリダイレクト

とすることができる

この辺で設定されている
https://github.com/rails/rails/blob/cf5f55cd30aef0f90300c7c8f333060fe258cd8a/actionpack/lib/action_dispatch/http/parameters.rb#L30-L44

params でも取れそうな気がするが、よくわからないがダメだった

0
1
0

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
  3. You can use dark theme
What you can do with signing up
0
1