Ruby on Rails - passing params into 301 redirect i

2019-06-18 03:51发布

问题:

I want to change my existing 'game' routing inside routes.rb, but because of SEO I need also to setup 301 redirect for old links.

My old routing:

match 'games/:permalink/:id/(:page)' => 'games#show'

New routing:

match 'gierki/:permalink/(:page)' => 'games#show'

Here is redirection which I tried to to do:

match 'games/:permalink/:id/(:page)' => redirect {|params| "/gierki/#{params[:permalink]}" + params[:page].nil? ? "" : "/#{params[:page]}" }

Above redirect is not working, here is an error:

wrong number of arguments (1 for 2)

回答1:

Try making it like this:

match 'games/:permalink/:id/(:page)' => redirect {|params,request| "/gierki/#{params[:permalink]}" + params[:page].nil? ? "" : "/#{params[:page]}" }

And see if it works.