routes question

Why does this link:

<h1>Pages#home</h1>
<p>Find me in app/views/pages/home.html.erb</p>

<%= link_to "Update clicks ", { :controller => 'users', :action =>
'update_clicks'} %>

...route me to the users/show page? Here are my routes:

Test2App::Application.routes.draw do
  resources :users
  root :to => "pages#home"

  post 'users/update_clicks'

I know I can get the link to go to 'pages/home' as intended by adding
:method => post to the link, but I'm curious how the the users/show page
gets rendered? I would expect a 'get' request to 'users/update_clicks'
to produce a routing error.

Here are my controllers:

class PagesController < ApplicationController
  def home
    @title = "Home"
  end
end

class UsersController < ApplicationController
  def update_clicks
    render 'pages/home'
  end
end

The users#show route which "resources :users" creates is:

    get '/users/:id', :controller => 'users', :action => 'show'

When you try to GET /users/update_clicks, it matches this route, with the 'id' parameter equal to 'update_clicks'.

There is nothing too magical going on in the router; it matches URL segments in a simple way. It doesn't know that 'update_clicks' is unlikely to be a real ID. All it sees is a path with 'users' in the first bit, and then *something* in the second bit, and as far as it's concerned that's a match.

More importantly, it just uses the first matching route it can find, and ignores anything else in your routes file. So in this case, it's finding a matching route in "resources :users", and so it's essentially ignoring your "post 'users/update_clicks'" route.

Chris

Thanks!