#46 Catch-all Route
Jun 18, 2007 | 5 minutes | Controllers, Routing
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.
Cool!
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)?
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
Hey.
Can I ask how you made that code snippet? Colorized i mean.
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".)
@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. :)
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
Hey Ryan.
Is this CodeRay stylesheet your made, and are we allowed to use it?
I like that theme.
@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.
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.
I'll probably do an episode in the future on adding coderay to a site.
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 ;)
It would be great to see how to integrate CodeRay to markdown (BlueCloth) or textilize (RedCloth).
Ryan please do a association in Active Record screen cast!
you are great :)
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!
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!
@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.
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
Per my last comment, I'm actually using movies and their titles instead of products ;)
Mike
Can Rails perform static resources rewrite as apache mod-rewrite?
btw,you voice is nice.
-RainChen
@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'
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
Loved the tutorial... Great. I am using many of your videos to learn more about rails... excellent idea of teaching stuff...
cheers
raghav..
I really appreciate this post. This was exactly what I needed, and it has saved me a lot of headache. Thanks for your assistance!
excellent voice , charming episode. nice example..do well in programming,etc. U great!
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.
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.
There is a simple utility that I have used which you can use for Rails Dynamic Routes (http://rdr.rubyforge.org/)
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.
Thank You for this video!
This episode has been updated for Rails 5 as a blog post. Catch All Route in Rails 5