#46
Jun 18, 2007

Catch-all Route

Sometimes you need to add complex/dynamic routes. This is often impossible to do in routes.rb, but do not worry. It can be accomplished with a catch-all route. See how in this episode.
Download (11.9 MB, 5:58)
alternative download for iPod & Apple TV (7.4 MB, 5:58)
# routes.rb
map.connect '*path', :controller => 'redirect', :action => 'index'

# redirect_controller.rb
def index
  product = Product.find(:first, :conditions => ["name LIKE ?", "#{params[:path].first}%"])
  redirect_to product_path(product)
end

RSS Feed for Episode Comments 28 comments

1. chineseGuy Jun 18, 2007 at 01:16

nice!~


2. Emil Jun 18, 2007 at 02:17

Cool!


3. weskycn Jun 18, 2007 at 03:51

你是一个高手,希望你能继续你的ROR之路!!


4. Luigi Jun 18, 2007 at 04:29

Thank you for your screenscasts!
I used a similar approch to localize the URLs.

Probably you don't feel this need, but what do you think about Rails localization project (es. Globalize-rails)?


5. Matt Holland Jun 18, 2007 at 04:40

I know a lot of people have also suggested putting the code snippets in with the episode, but thanks a lot for implementing my suggestion Ryan. :) Shall make things a whole lot easier.

Thanks
Matt


6. HeyHey Jun 18, 2007 at 05:53

Hey.
Can I ask how you made that code snippet? Colorized i mean.


7. Brian Jun 18, 2007 at 06:45

These Railscasts are great, every one of them.

Adding code snippets to your website will be a real helpful addition. (I think there is a typo in today's code snippet though - remove the "]" after "find".)


8. Ryan Bates Jun 18, 2007 at 07:25

@Luigi, I haven't used the globalize plugin, so I can't comment on it. I might play around with different localization techniques some day and make a screencast about it though.

@HeyHey, I used the coderay gem to make it colorized.

@Brian, fixed the typo.

Thanks for the comments everyone. :)


9. Gabe da Silveira Jun 18, 2007 at 08:15

My favorite use of this technique is to link to static content. That way my static page URLs can stay really clean: http://darwinweb.net/article/Routing_Nested_Static_Content_In_Rails


10. Hey Jun 18, 2007 at 09:36

Hey Ryan.
Is this CodeRay stylesheet your made, and are we allowed to use it?

I like that theme.


11. Ryan Bates Jun 18, 2007 at 10:11

@Hey, yeah, the stylesheet is custom made, but you are free to use it wherever you want.

http://railscasts.com/stylesheets/coderay.css

It's not perfect and it's just made to work with ruby and rhtml. Other languages might not look so hot.


12. Hey Jun 18, 2007 at 10:36

Hey. Thanks.

I'm disturbing again.
I made little helper for coderay. It works well, but i think it's a bit ugly code or what you think:
http://pastie.caboo.se/71442

Some railscasts about helper methods would be great :P.


13. Ryan Bates Jun 18, 2007 at 11:36

I'll probably do an episode in the future on adding coderay to a site.


14. Karl Smith Jun 18, 2007 at 11:51

WOW did I learn something helpful... the '.inspect' method. What a great tool for debugging! Along with the 'render :text => foo.inspect', my life just got way easier.

Suggestion/request: a screen cast on various methods of debugging?

Oh ya, the routest thing was nice too ;)


15. Tobias Jun 19, 2007 at 12:32

It would be great to see how to integrate CodeRay to markdown (BlueCloth) or textilize (RedCloth).


16. some dude Jun 19, 2007 at 15:54

Ryan please do a association in Active Record screen cast!

you are great :)


17. Chu Yeow Jun 19, 2007 at 19:31

I've seen a similar routing setup in Mephisto but didn't really grok it. Your screencast really made it really clear and simple! Very cool, thanks!


18. urbankid Jun 22, 2007 at 02:21

Keep up the good work!


19. Warren W Jun 26, 2007 at 12:44

Ryan, **excellent** work on these screen casts... brilliant, helpful, and bite-sized!

One thing that I am curious about is setting up a rails app that runs like reddit, where the subdomain prefix is used to scope content (programming.reddit.com versus www.reddit.com)... is this possible with routing in rails, or is it better to build multiple app copies? Maybe a screen cast about something like this, if it's applicable?

Thanks again for your hard work!


20. Ryan Bates Jun 26, 2007 at 13:56

@Warren, there's a plugin out there which allows you to use subdomains in route conditions.

http://weblog.rubyonrails.org/2006/5/22/dan-webbs-request-routing-plugin

I'm not sure if it's fully compatible with Rails 1.2.3, but it might get you partway there.

I think it's best to keep this all in one app. That way it's easier to share the tables, logic, and mongrel instances.


21. Mike Stramba Jul 08, 2007 at 04:56

Ryan,

This is pretty cool, a "shortcut" search instead of waiting for a form to load/ submit etc ... just like typical unix command line stuff ;)

However, one thing you didn't show in your railscast is what happens if you try to find a product that doesn't exist .
Or maybe I've messed something up on my setup.

If I try "http://lh:3000/gl" for example, and none of my products start with "gl" I get a routing error

http://pastie.caboo.se/77040

Mike


22. Mike Stramba Jul 08, 2007 at 04:57

Per my last comment, I'm actually using movies and their titles instead of products ;)

Mike


23. RainChen Jul 08, 2007 at 06:46

Can Rails perform static resources rewrite as apache mod-rewrite?

btw,you voice is nice.

-RainChen


24. Ryan Bates Jul 08, 2007 at 08:12

@Mike, the routing error you get will be a 404 error in production mode, so I think that fits rather well. Of course you can also check if no product was found and have it behave however you need it to.

@RainChen, not exactly sure what you mean. If you're using Apache you can still use mod-rewrite where you need it. Or perhaps you're looking for a custom route instead?

map.foo "/foo", :controller => 'bar', :action => 'foo'


25. Michael Behan Aug 24, 2007 at 12:24

Nice idea! This method worked a little better for me (since '/' characters are cut from params[:path]):

def index
  redirect_to "http://www.mydomain.com#{request.request_uri}"
end


26. Raghav Feb 29, 2008 at 22:20

Loved the tutorial... Great. I am using many of your videos to learn more about rails... excellent idea of teaching stuff...

cheers
raghav..


27. Mark Barber May 03, 2008 at 18:07

I really appreciate this post. This was exactly what I needed, and it has saved me a lot of headache. Thanks for your assistance!


28. kino May 23, 2008 at 01:51

As I have elsewhere shown, our faculties exclude the possibility of necessity, by virtue of practical reason.

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player