ruby-on-railsroutescustom-routesreroute

Route old ids to new routes with the same controller name and using friendly id


I have a rails app but I need to route old routes from a php app to my new rails routes (because of old google ads tied to them). I have looked far and wide and I can't figure out how to solve my problem.

I have a videos controller where my new videos use "friendly id". Now, I have old routes, where they use both slugs and ids. I need to redirect these old routes to my new routes. I know that I need to check is the video is nil, but I'm trying to figure out how to define what slug goes where.

My issue: I am trying to do the simple reroute thing, but my controller is trying to located the id and is unable to find them (since the slugs are no longer valid) and it throws an error. I have a list of 6 videos that I need to reroute to my new routes.

Any suggestions on what I'm missing?

Routes

get '/videos/toys' => redirect('/videos')
get '/videos/88' => redirect('/videos/cool-video')
get '/videos/1388' => redirect('/videos/better-video')
get '/videos/tech-video' => redirect('/videos/technologies-video')

For example, I have a tech-video, old slug, that needs to go to the new "technologies video" or I have an id of '88' that needs to go to "cool-video"

Controller

(bottom of controller)
private

def set_video
  @video = Video.friendly.find(params[:id])
  if @video.nil?
    **method goes here**

  end
end

Solution

  • As we found out in chat, make sure that no routes with 'videos'

    (including resources :videos)

    occur in the routes file above your redirects as a route containing /videos/:id will get matched before the redirect kicks in.

    Good luck!!