Routing and Parameters

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
%>
  <button>go</button>
<% 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

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
%>
<button>go</button>
<% 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?

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

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

James

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

James Herdman wrote:

Hi Will!

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

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