On 12 Aug 2011, at 21:07, 7stud -- wrote:
> 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.
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
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.