# Named route not working -- input hash completely ignored

**URL:** <https://www.ruby-forum.com/t/named-route-not-working-input-hash-completely-ignored/132673>\
**Category:** Rails\
**Created:** [March 23, 2008, 1:30am UTC](https://www.ruby-forum.com/t/named-route-not-working-input-hash-completely-ignored/132673 "2008-03-23T01:30:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbobker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@tbobker](https://www.ruby-forum.com/u/tbobker)\
**Post date:** [March 23, 2008, 1:30am UTC](https://www.ruby-forum.com/t/named-route-not-working-input-hash-completely-ignored/132673/1 "2008-03-23T01:30:31Z")

</div>

I have been reading that you can pass a parameters hash to a named  
route:

You can get \<%= link\_to "help!", assist\_path(:action =\> "membership") %\>

You can get [help!](https://www.ruby-forum.com/assistance/membership)

Now, for me, I have the following named route:

# the bootcamp calendar

map.bootcamp\_calendar “/boot\_camp\_info/Calendar/:year/:month”,  
:controller =\> “boot\_camp\_info”,  
:action =\> “Calendar”,  
:requirements =\> {:year =\> /\d{4}/,:month =\> /\d{1,2}/},  
:defaults =\> {:year =\> Date.today.year, :month =\>  
Date.today.month}

However, my code

\<%= link\_to "Prev", bootcamp\_calendar\_path(:year =\> '2008', :month =\> '3') %\>

Produces [Prev](https://www.ruby-forum.com/boot_camp_info/Calendar) not [Prev](https://www.ruby-forum.com/boot_camp_info/Calendar/2008/3)

In fact, when debugging, bootcamp\_calendar\_path(:year =\> ‘2008’, :month  
=\> ‘3’) resolves to /boot\_camp\_info/Calendar and neglects the hash  
values.

I have fought with this for awhile, but I am at a loss. I have traced  
all the rails code I can find, but don’t see where things could be going  
wrong.

Any help greatly appreciated.

regards,

tim

---

<div class="post-metadata">

**Author:** ![tbobker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@tbobker](https://www.ruby-forum.com/u/tbobker)\
**Post date:** [March 23, 2008, 2:33am UTC](https://www.ruby-forum.com/t/named-route-not-working-input-hash-completely-ignored/132673/2 "2008-03-23T02:33:39Z")

</div>

Hi –

On Sun, 23 Mar 2008, Tim B. wrote:

> map.bootcamp\_calendar “/boot\_camp\_info/Calendar/:year/:month”,  
> :controller =\> “boot\_camp\_info”,  
> :action =\> “Calendar”,

It’s much better to use lowercase names for methods. Uppercase names  
are legal (and there are a few even built into Ruby), but they’re very  
unidiomatic for controller actions (and most purposes).

> ```
> :requirements => {:year => /\d{4}/,:month => /\d{1,2}/},
> :defaults => {:year => Date.today.year, :month =>
> 
> ```
> 
> Date.today.month}
> 
> However, my code
> 
> \<%= link\_to "Prev", bootcamp\_calendar\_path(:year =\> '2008', :month =\> '3') %\>
> 
> Produces [Prev](https://www.ruby-forum.com/boot_camp_info/Calendar) not [Prev](https://www.ruby-forum.com/boot_camp_info/Calendar/2008/3)

There’s no need for the defaults to be put explicitly in the path,  
because when you click on the version without them, they’ll be  
interpolated anyway. It’s an example of the fact that the routing  
system only makes sense to itself. When it produces a path string,  
it’s producing a string that may not have a lot of human-readable  
info, but that will be understood (including interpolation of  
defaults) by the URL recognition process.

David

–  
Upcoming Rails training from David A. Black and Ruby Power and Light:  
ADVANCING WITH RAILS April 14-17 New York City  
INTRO TO RAILS June 9-12 Berlin  
ADVANCING WITH RAILS June 16-19 Berlin  
CORE RAILS June 24-27 London (Skills Matter)  
See [http://www.rubypal.com](http://www.rubypal.com) for more info!

---

<div class="post-metadata">

**Author:** ![tbobker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@tbobker](https://www.ruby-forum.com/u/tbobker)\
**Post date:** [March 23, 2008, 1:30pm UTC](https://www.ruby-forum.com/t/named-route-not-working-input-hash-completely-ignored/132673/3 "2008-03-23T13:30:59Z")

</div>

Tim B. wrote:

> I am not trying to put defaults in the path – I am trying to link to a  
> specific month and year. I really need  
> href="/boot\_camp\_info/Calendar/2008/3". With my named route, I would  
> really like to get bootcamp\_calendar\_url(:year =\> @myyear, :month =\>  
> @mymonth) to point to a specific year and month.

As David said, Rails doesn’t need this because of your defaults. These  
mean that:

/boot\_camp\_info/Calendar

will be filled in with the default year and month, so if you try to  
create a link for the current year and month, Rails knows these are not  
required in the link so doesn’t include them.

Rails uses all the information in your routes file to determine an  
appropriate URL for your request. Because you are specifying defaults,  
you are telling Rails that those values are optional, so Rails knows it  
can generate URLs without them and the result will be meaningful.

If the year and month must always be included in the URL, then don’t  
supply defaults and always specify the values when creating your URLs.

---

<div class="post-metadata">

**Author:** ![tbobker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@tbobker](https://www.ruby-forum.com/u/tbobker)\
**Post date:** [March 23, 2008, 12:53pm UTC](https://www.ruby-forum.com/t/named-route-not-working-input-hash-completely-ignored/132673/4 "2008-03-23T12:53:56Z")

</div>

David,

Thank you so much for your reply. However, after reading your message  
several times, I am confused what you are recommending. Got it on the  
capitalization of the controller, put that in their early before I  
understood rails naming conventions. You don’t think that is causing the  
problem, correct?

I am not trying to put defaults in the path – I am trying to link to a  
specific month and year. I really need  
href=“/boot\_camp\_info/Calendar/2008/3”. With my named route, I would  
really like to get bootcamp\_calendar\_url(:year =\> @myyear, :month =\>  
@mymonth) to point to a specific year and month.

I have this working via a helper I made, I know I could even do:

\<%= link\_to “Next month”,  
“#{bootcamp\_calendar\_path}/#{@myyear}/#{@mymonth}” %\>

but after reading the documentation, I think  
bootcamp\_calendar\_path(:year =\> ‘2008’, :month =\> ‘3’) should work and I  
am really nervous that rails says it can do something that it doesn’t do  
– and no errors are returned. so i have things working, but I am  
worried that other things in rails might not be working as well. Do I  
have a legitimate bug here – or am i doing something wrong?

best,

tim

David A. Black wrote:

> Hi –
> 
> On Sun, 23 Mar 2008, Tim B. wrote:
> 
> > map.bootcamp\_calendar “/boot\_camp\_info/Calendar/:year/:month”,  
> > :controller =\> “boot\_camp\_info”,  
> > :action =\> “Calendar”,
> 
> It’s much better to use lowercase names for methods. Uppercase names  
> are legal (and there are a few even built into Ruby), but they’re very  
> unidiomatic for controller actions (and most purposes).
> 
> > ```
> > :requirements => {:year => /\d{4}/,:month => /\d{1,2}/},
> > :defaults => {:year => Date.today.year, :month =>
> > 
> > ```
> > 
> > Date.today.month}
> > 
> > However, my code
> > 
> > \<%= link\_to "Prev", bootcamp\_calendar\_path(:year =\> '2008', :month =\> '3') %\>
> > 
> > Produces [Prev](https://www.ruby-forum.com/boot_camp_info/Calendar) not [Prev](https://www.ruby-forum.com/boot_camp_info/Calendar/2008/3)
> 
> There’s no need for the defaults to be put explicitly in the path,  
> because when you click on the version without them, they’ll be  
> interpolated anyway. It’s an example of the fact that the routing  
> system only makes sense to itself. When it produces a path string,  
> it’s producing a string that may not have a lot of human-readable  
> info, but that will be understood (including interpolation of  
> defaults) by the URL recognition process.
> 
> David
> 
> –  
> Upcoming Rails training from David A. Black and Ruby Power and Light:  
> ADVANCING WITH RAILS April 14-17 New York City  
> INTRO TO RAILS June 9-12 Berlin  
> ADVANCING WITH RAILS June 16-19 Berlin  
> CORE RAILS June 24-27 London (Skills Matter)  
> See [http://www.rubypal.com](http://www.rubypal.com) for more info!

---

<div class="post-metadata">

**Author:** ![tbobker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@tbobker](https://www.ruby-forum.com/u/tbobker)\
**Post date:** [March 23, 2008, 2:09pm UTC](https://www.ruby-forum.com/t/named-route-not-working-input-hash-completely-ignored/132673/5 "2008-03-23T14:09:19Z")

</div>

Hi –

On Sun, 23 Mar 2008, Tim B. wrote:

> specific month and year. I really need  
> bootcamp\_calendar\_path(:year =\> ‘2008’, :month =\> ‘3’) should work and I  
> am really nervous that rails says it can do something that it doesn’t do  
> – and no errors are returned. so i have things working, but I am  
> worried that other things in rails might not be working as well. Do I  
> have a legitimate bug here – or am i doing something wrong?

Definitely not a bug. What you’re not taking into account (and see  
Mark’s answer too) is that the routing system talks to itself.

Given the defaults you’ve specified, when you click on this:

“/boot\_camp\_info/calendar”

the routing system knows that you mean:

controller: boot\_camp\_info  
action: calendar  
params[:year]: “2008”  
params[:month]: “3”

That’s what it means for there to be defaults. It doesn’t mean that  
they get inserted physically into the path; it means that _without_  
having them physically present, the routing system knows that they’re  
what you want.

The routing system uses your routing rule to:

1. write the path string;
2. interpret a request URL that contains the path string.

Your rule specifies defaults; therefore, the system knows how to  
interpret a request that has no explicit values in the :year and  
:month positions. So that’s what it writes, as the path, when it is  
asked to use the defaults.

David

–  
Upcoming Rails training from David A. Black and Ruby Power and Light:  
ADVANCING WITH RAILS April 14-17 New York City  
INTRO TO RAILS June 9-12 Berlin  
ADVANCING WITH RAILS June 16-19 Berlin  
CORE RAILS June 24-27 London (Skills Matter)  
See [http://www.rubypal.com](http://www.rubypal.com) for more info!
