Praveenkumar
Praveenkumar

Reputation: 981

Redirect in routes.rb with wildcard values instead of params?

To redirect in routes.rb with params we use following syntax.

get '/stories/:name', to: redirect('/articles/%{name}', status: 301)

If I want to pass the wild card url How can i do it. I tried the following it is not working.

get '/stories/*search', to: redirect('/articles/%{search}', status: 302)

get '/stories/*search', to: redirect("/articles/#{search}", status: 302)

I there any hack around for this .

Upvotes: 0

Views: 367

Answers (1)

user3366016
user3366016

Reputation: 1312

I believe one of these 3 solutions should work for you, but it's a little hard to tell exactly what you're after.

This will basically switch out stories with articles and keep the wild card value of the url. i.e. /stories/hello-world will become /articles/hello-world

get '/stories/*name', to: redirect('/articles/%{name}', status: 302)

or if you want to pass the whole '/stories/*name' original url

get '/stories/*name', to: redirect('/articles/stories/%{name}', status: 302)

or if you are wanting to pass the wildcard as a query string you could do it like this

get '/stories/*name', to: redirect('/articles/?name=%{name}', status: 302)

You can find examples in the documentation. I've combined 3.11 and 3.12.

Upvotes: 1

Related Questions