goutham_kgh
goutham_kgh

Reputation: 167

Rails: How to redirect all urls from previous model to the new model

I am new to Ruby on Rails and I'm developing a sample app. I have a model called Doi. Since it seems less intuitive, I renamed the model to Destination.

I'm stuck with one main issue: how to redirect all the urls that contained the old model Doi to the new model Destination.

Example:

    0.0.0.0:3000/<action_x>/dois/<action_y> 

should automatically redirect to

    0.0.0.0:3000/<action_x>/destinations/<action_y>

In my routes.rb, I have used:

    match '/**/dois/**' => redirect('/**/destinations/**')

But it didn't work. What is the correct way to go about this ?

Upvotes: 3

Views: 120

Answers (1)

Baldrick
Baldrick

Reputation: 24340

You can try with

match '/:before/dois/:after' => redirect('/%{before}/destinations/%{after}')

See more examples of dynamic redirection in the Rails Routing Guide


Another solution, more customisable, would be:

match '*before/dois/*after' => redirect {|params| "#{params[:before]}/destinations/#{params[:after]}" }

More help with wildcards also in the Rails Routing Guide

Upvotes: 2

Related Questions