1

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
AGirlThatCodes
  • 575
  • 7
  • 21
  • I haven't used `friendly_id` before, but think you may just need to add a route below the routes you have that looks like `get '/videos/:id' => 'controller#action'` where `controller` is probably `videos` and `action` is whatever your controller action is (probably `show`) – dinjas Dec 10 '15 at 04:29
  • I hate to play into the "dumb blonde" sterotype, but I can't follow your suggestion completely because I can't get my head around it. Based on the routes I have defined above, what type of route on route would you suggest? – AGirlThatCodes Dec 10 '15 at 04:32
  • When rails looks for a route to match, it will see the routes you have `/videos/88`, `/videos/1388`, etc and redirect them for you as you've requested, but since you don't have a route defined for `/videos/cool-video`, and `/videos/better-video`, it doesn't know what to do. So, if you add that `/videos/:id` route below the ones you have, Rails will see that one and match it and will assume that `cool-video` and `better-video` are for the `:id` field and will make them available in your controller as params[:id] – dinjas Dec 10 '15 at 04:38
  • The new route would be `get '/videos/:id' => 'videos#show'` (if you want the user to be sent to the `show` action in your `VideosController`. You'll probably need to change the right side of your `/videos/toys` route also as it doesn't have anything to use for `params[:id]` – dinjas Dec 10 '15 at 04:39
  • Question, the ids on the left are old "ids" and are not tied to any videos. the ids on the right are the current routes. How do I 'change/transform/match' the old ones with the new ones. Friendly id just makes the ids in strings instead of numbers. The ids on the right are the active slugs. I only ask because I'm getting an error when I do a uniform route that the one you suggested. – AGirlThatCodes Dec 10 '15 at 04:55
  • Right. The routes on the right are tied to videos though, right? E.g. you want `/videos/cool-video` to show the video with the slug: "cool-video"? You'll also want to make sure you don't have any videos routes above your redirects, e.g. `resources :videos` (those routes would be matched before the redirect routes). Also, your redirect syntax may be a bit off. (http://guides.rubyonrails.org/routing.html#redirection) `get '/videos/88', to: redirect('/videos/cool-video')` – dinjas Dec 10 '15 at 05:05
  • So, I updated my routes with your suggestion. I even tried doing a resource/do block (probably wrong but thought why not) and I am still getting an ```ActiveRecord::RecordNotFound``` error because it's hitting the controller method: ```def set_video @video = Video.friendly.find(params[:id]) end``` – AGirlThatCodes Dec 10 '15 at 05:16
  • I need /videos/88 (id:88) to go to 'cool video' with the (id/slug:'cool-video') – AGirlThatCodes Dec 10 '15 at 05:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97467/discussion-between-dinjas-and-agirlthatcodes). – dinjas Dec 10 '15 at 05:20

1 Answers1

0

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!!

dinjas
  • 2,115
  • 18
  • 23