# Routing and Parameters

**URL:** <https://www.ruby-forum.com/t/routing-and-parameters/142233>\
**Category:** Rails\
**Created:** [July 4, 2008, 9:47pm UTC](https://www.ruby-forum.com/t/routing-and-parameters/142233 "2008-07-04T21:47:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [July 4, 2008, 9:47pm UTC](https://www.ruby-forum.com/t/routing-and-parameters/142233/1 "2008-07-04T21:47:38Z")

</div>

Greetings. I have a route something like this:

map.calendar ‘/tasks/:year/:month’,  
:controller =\> ‘tasks’,  
:action =\> ‘calendar’,  
:year =\> /\d{4}/,  
:month =\> /\d{2}/

(Ignore the obvious problems of invalid data.)

I’m trying to create a form that will allow my users to navigate from  
one month/year to the next. Here’s my form:

\<% form\_tag(tasks\_path, :method =\> :get) do %\>  
\<%= select\_month(Date.today, :prefix =\> ‘month’, :discard\_type =\>  
true %\>  
\<%= select\_year(Date.today, :prefix =\> ‘year’, :discard\_type =\> true  
%\>  
go  
\<% end %\>

If you submit this form, the request generates an URL like this: /  
tasks?month=7&year=2008 sent to the #index method on TasksController.

Why isn’t this recognized as a request to the #calendar action? What  
am I doing wrong?

James

---

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [July 4, 2008, 11:03pm UTC](https://www.ruby-forum.com/t/routing-and-parameters/142233/2 "2008-07-04T23:03:57Z")

</div>

> I’m trying to create a form that will allow my users to navigate from  
> If you submit this form, the request generates an URL like this: /  
> tasks?month=7&year=2008 sent to the #index method on TasksController.
> 
> Why isn’t this recognized as a request to the #calendar action? What  
> am I doing wrong?

Route generation happens on the server side. The above HTML/view is  
equivalent to this as far as Rails routing is concerned:

> \<% form\_tag(tasks\_path, :method =\> :get) do %\>  
> \<% end %\>

Rails is properly generating the url for “tasks\_path” and your browser  
is submitting to that a GET request with whatever form elements are in  
that form.

-philip

---

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [July 5, 2008, 1:02am UTC](https://www.ruby-forum.com/t/routing-and-parameters/142233/3 "2008-07-05T01:02:44Z")

</div>

So, when the request /tasks?year=2008&month=8 comes in, why doesn’t  
Rails  
recognize this as /tasks/2008/8?

James

---

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [July 5, 2008, 8:51am UTC](https://www.ruby-forum.com/t/routing-and-parameters/142233/4 "2008-07-05T08:51:06Z")

</div>

Hi Will!

No offence taken. My knowledge of the router had a gap and this is  
where it  
was at, hence the original message 🙂

Thanks for the info. I’d prefer to avoid a two-stage solution, but I  
have a  
feeling there isn’t much of a way around it.

Many thanks.

James

---

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [July 5, 2008, 3:36am UTC](https://www.ruby-forum.com/t/routing-and-parameters/142233/5 "2008-07-05T03:36:29Z")

</div>

James, At the risk of being rude, the problem is that you don’t  
understand how routing works.

1. map.calendar … would give you a calendar\_path(:year, :month)  
function, not task\_path. If you have a task\_path function, it is being  
generated from a different rule.

2. In the request /tasks?year=2008&month=8, everything after the  
question mark is just data generated from the form, and is invisible to  
the routing.

3. To get the routing you were trying for, you would need:  
\<% form\_tag(calendar\_path(year, month), :method =\> :get) do %\>  
The problem you have is that the form tag is evaluated when the form is  
generated, before it is sent to the browser. This means that the user  
data can’t be used this way. To do what you want to do, you will need a  
different algorithm.

Probably you want a two stage approach. Have one action that evaluates  
the user data, and then redirects to the desired action.

Hope that helps,

–Will Merrell
