# Optional routing URL component?

**URL:** <https://www.ruby-forum.com/t/optional-routing-url-component/114190>\
**Category:** Rails\
**Created:** [September 9, 2007, 2:00pm UTC](https://www.ruby-forum.com/t/optional-routing-url-component/114190 "2007-09-09T14:00:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stubbsuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e56c9b/32.png) [@stubbsuk](https://www.ruby-forum.com/u/stubbsuk)\
**Post date:** [September 9, 2007, 2:00pm UTC](https://www.ruby-forum.com/t/optional-routing-url-component/114190/1 "2007-09-09T14:00:01Z")

</div>

For legacy reasons, I’d like to be able to have part of a url as  
optional, the problem is that it’s at the start of the URL, rather  
than the end.

/laddr/match/result

with ‘/laddr’ being optional, depending on which URL the user uses to  
access the site.

Is it possible to do this with routes?

---

<div class="post-metadata">

**Author:** ![stubbsuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e56c9b/32.png) [@stubbsuk](https://www.ruby-forum.com/u/stubbsuk)\
**Post date:** [September 9, 2007, 2:31pm UTC](https://www.ruby-forum.com/t/optional-routing-url-component/114190/2 "2007-09-09T14:31:08Z")

</div>

Stuart G. wrote:

> For legacy reasons, I’d like to be able to have part of a url as  
> optional, the problem is that it’s at the start of the URL, rather  
> than the end.
> 
> /laddr/match/result
> 
> with ‘/laddr’ being optional, depending on which URL the user uses to  
> access the site.
> 
> Is it possible to do this with routes?

of course, everything is possible with RubyonRails, don’t make this  
mistake and ask such a question again 😛 _just kidding_

map.connect /:option/match/result, :controller =\> “something”, :action  
=\> “methodname”, :option =\> /.\*/

Try that 😃

---

<div class="post-metadata">

**Author:** ![stubbsuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e56c9b/32.png) [@stubbsuk](https://www.ruby-forum.com/u/stubbsuk)\
**Post date:** [September 10, 2007, 10:20pm UTC](https://www.ruby-forum.com/t/optional-routing-url-component/114190/3 "2007-09-10T22:20:36Z")

</div>

On Sep 9, 1:31 pm, Jamal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > Is it possible to do this with routes?
> 
> of course, everything is possible with RubyonRails, don’t make this  
> mistake and ask such a question again 😛 _just kidding_

heh 🙂

> map.connect /:option/match/result, :controller =\> “something”, :action  
> =\> “methodname”, :option =\> /.\*/
> 
> Try that 😃

Cool, that works, in most cases. Just need to figure out the  
accompanying apache rewrite rules to serve any actual files from te  
directory, instead of pointing them at mongerl.

Cheers Jamal …

---

<div class="post-metadata">

**Author:** ![stubbsuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e56c9b/32.png) [@stubbsuk](https://www.ruby-forum.com/u/stubbsuk)\
**Post date:** [September 10, 2007, 10:21pm UTC](https://www.ruby-forum.com/t/optional-routing-url-component/114190/4 "2007-09-10T22:21:58Z")

</div>

Stuart G. wrote:

> On Sep 9, 1:31 pm, Jamal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > > Is it possible to do this with routes?
> > 
> > of course, everything is possible with RubyonRails, don’t make this  
> > mistake and ask such a question again 😛 _just kidding_
> 
> heh 🙂

You are welcome Stuart 😃

---

<div class="post-metadata">

**Author:** ![stubbsuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e56c9b/32.png) [@stubbsuk](https://www.ruby-forum.com/u/stubbsuk)\
**Post date:** [September 17, 2007, 9:17pm UTC](https://www.ruby-forum.com/t/optional-routing-url-component/114190/5 "2007-09-17T21:17:23Z")

</div>

So, while this works great on the productions server (Apache2 +  
mongrel\_cluster) it causes a few problems when running on the dev  
machine.

Can I have a seperate set of routes for the dev box? or am I missing  
something?

This is what is surrently in my routes.rb :

map.connect ‘/:option’, :controller =\> “welcome”, :option =\> /laddr/

map.connect ‘/:option/:action/:defender\_id/  
ladder/:ladder\_id’, :controller =\> ‘ladder’, :option =\> /laddr/  
map.connect ‘/:option/match/refuse/:id’, :controller =\>  
‘match’, :action =\> ‘refuse’, :option =\> /laddr/  
map.connect ‘/:option/match/result/:id’, :controller =\>  
‘match’, :action =\> ‘result’, :option =\> /laddr/  
map.connect ‘/:option/match/:match\_id/:action/:id’, :controller =\>  
‘match’, :option =\> /laddr/

# Install the default route as the lowest priority.

map.connect ‘/:option/:controller/:action/:id.:format’, :option =\> /  
laddr/  
map.connect ‘/:option/:controller/:action/:id’, :option =\> ‘/laddr/’

The optional bit, doesn’t seem to be optional, so if I do  
[http://localhost:3000/](http://localhost:3000/)  
I get an error :

“no route found to match “/” with {:method=\>:get}”

but when I add the /laddr it works fine. I can live with that, but it  
also breaks static files, stylesheets and images for example, as it’s  
now looking for those in /laddr/stylesheets

I don’t really like the idea of having 2 sets of routes, because,  
well, I only want to maintain 1 and not forget about the production  
ones all the time (because you know that’s what will happen).

-S

---

<div class="post-metadata">

**Author:** ![stubbsuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e56c9b/32.png) [@stubbsuk](https://www.ruby-forum.com/u/stubbsuk)\
**Post date:** [September 26, 2007, 9:29pm UTC](https://www.ruby-forum.com/t/optional-routing-url-component/114190/6 "2007-09-26T21:29:37Z")

</div>

On Sep 17, 8:16 pm, Stuart Grimshaw [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> map.connect ‘/:option/:controller/:action/:id.:format’, :option =\> /  
> now looking for those in /laddr/stylesheets
> 
> I don’t really like the idea of having 2 sets of routes, because,  
> well, I only want to maintain 1 and not forget about the production  
> ones all the time (because you know that’s what will happen).

I’m still stuck with this if anyone has any ideas?
