8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails の Routing でパラメータを残してリダイレクトする

Last updated at Posted at 2014-07-16

/old?a=3/new?a=3 にするような場合です。

get '/old' => redirect('/new')

だと、/old?a=3/new になってしまいます。

これを解決するには、キーワード引数形式で redirect メソッドを呼びます。

get '/old' => redirect(path: '/new')

なお、レスポンスコードは、デフォルトで 301 になります。

サンプル

/tokyo/food?a=3 を /food?a=3 にするような場合です。

area_path = Regexp.union(Area.pluck(:name))
category_path = Regexp.union(Category.pluck(:name))

get(':area/:category' => redirect(path: '/%{category}'),
  area: area_path, category: category_path)
8
7
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
8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?