#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 26 comments

1. chineseGuy Jun 18, 2007 at 01:16

nice!~


2. Emil Jun 18, 2007 at 02:17

Cool!


3. 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)?


4. 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


5. HeyHey Jun 18, 2007 at 05:53

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


6. 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".)


7. 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. :)


8. 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


9. 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.


10. 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.


11. 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.


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

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


13. 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 ;)


14. Tobias Jun 19, 2007 at 12:32

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


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

Ryan please do a association in Active Record screen cast!

you are great :)


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


17. urbankid Jun 22, 2007 at 02:21

Keep up the good work!


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


19. 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.


20. 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


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

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

Mike


22. RainChen Jul 08, 2007 at 06:46

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

btw,you voice is nice.

-RainChen


23. 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'


24. 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


25. 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..


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


27. elite Oct 16, 2008 at 06:30

excellent voice , charming episode. nice example..do well in programming,etc. U great!


28. jc Dec 10, 2008 at 09:48

I think I'd use the XSS plugin if you're going to do this. I'd worry about security since you're giving users direct access to an sql query.


29. Nilesh Dec 16, 2008 at 00:00

here is something that you might find useful. This is attempt to make routes.rb more dynamic. routes.rb gets dynamically generated without need of server restart or redeploy.


30. Hit Feb 13, 2009 at 00:16

There is a simple utility that I have used which you can use for Rails Dynamic Routes (http://rdr.rubyforge.org/)


31. korinthe Mar 25, 2010 at 10:34

Thanks Ryan. I was having a problem -- wanted to have a drop-down menu (to select a Person to edit) and a button that said "Jump" to take you there -- and had just discovered that Rails couldn't generate the form html without knowing the id of the Person. Got a RoutingError instead.

I had been wondering whether a dispatcher-style redirect action would be a "nice" solution to this. Thanks for explaining it here.


32. free directory list Aug 11, 2010 at 22:19

you for sharing.Nice post


33. p90 workout Aug 12, 2010 at 09:16

Yes, thats exactly what I wanted to hear! Great stuff here. The information and the detail were just perfect. I think that your perspective is deep, its just well thought out and really fantastic to see someone who knows how to put these thoughts down so well. Great job on this.


34. Air Jordan Retro Aug 14, 2010 at 01:35

I think I'd use the XSS plugin if you're going to do this. This is pretty cool, a "shortcut" search instead of waiting for a form to load/ submit etc ... just like typical unix command line stuff ;)


35. Discount Timberland uk Aug 24, 2010 at 00:30

cool,nice post


36. louis vuitton shoes Aug 26, 2010 at 21:02

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


37. snow boots Aug 31, 2010 at 01:32

Your screencast really made it really clear and simple! Very cool, thanks!


38. GHD Australia Sep 01, 2010 at 01:19

Good choice to come here


39. GHD Hair Straighteners Sep 01, 2010 at 01:21

I hope to view more such articles


40. Cheap Supra Shoes Sep 01, 2010 at 01:23

Good article


41. louis vuitton sunglasses Sep 01, 2010 at 21:20

I agree with your Blog and I will be back to check it more in the future so please keep up your work. I love your content & the way that you write. It looks like you’ve been doing this for a while now, how long have you been blogging for?

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
Give Back to Open Source