Routing issue/bug?

For some reason the following form always sends me to the index action

<% form_for :league, :url => admin_leagues_url do |f| %>

<% end %>

If I set the :url to leagues_url (to which is also set in the routes
file) I am routed properly to the create action.

Here is the rendered form:

...

Here is part of my routing file:
map.namespace :admin do |admin|
admin.resources :leagues
end

Is this a bug or am I missing something?

Thanks for the help.

You can try out rake routes to see how your routes.rb is working.

Everything seems to be as expected (not sure how readable this will be):

          admin_leagues GET    /admin/leagues 

{:controller=>“admin/leagues”, :action=>“index”}
formatted_admin_leagues GET /admin/leagues.:format
{:controller=>“admin/leagues”, :action=>“index”}
POST /admin/leagues
{:controller=>“admin/leagues”, :action=>“create”}
POST /admin/leagues.:format
{:controller=>“admin/leagues”, :action=>“create”}
new_admin_league GET /admin/leagues/new
{:controller=>“admin/leagues”, :action=>“new”}

formatted_new_admin_league GET /admin/leagues/new.:format
{:controller=>“admin/leagues”, :action=>“new”}
edit_admin_league GET /admin/leagues/:id/edit
{:controller=>“admin/leagues”, :action=>“edit”}
formatted_edit_admin_league GET /admin/leagues/:id/edit.:format
{:controller=>“admin/leagues”, :action=>“edit”}
admin_league GET /admin/leagues/:id
{:controller=>“admin/leagues”, :action=>“show”}
formatted_admin_league GET /admin/leagues/:id.:format
{:controller=>“admin/leagues”, :action=>“show”}
PUT /admin/leagues/:id
{:controller=>“admin/leagues”, :action=>“update”}
PUT /admin/leagues/:id.:format
{:controller=>“admin/leagues”, :action=>“update”}
DELETE /admin/leagues/:id
{:controller=>“admin/leagues”, :action=>“destroy”}
DELETE /admin/leagues/:id.:format
{:controller=>“admin/leagues”, :action=>“destroy”}
POST /session
{:controller=>“sessions”, :action=>“create”}

That’s not readable…

Do you find your admin_leagues is listed under POST and create method?

I can’t find them.

Are you sure you have “admin/leagues” controller? in
/app/controllers/admin/leagues_controller.rb ?

On Sun, Jun 15, 2008 at 12:00 AM, Chris O. <
[email protected]> wrote:

For some reason the following form always sends me to the index action

<% form_for :league, :url => admin_leagues_url do |f| %>

<% end %>

I believe that the preferred way to do this is

form_for([:admin, @league]) do |f|

Assuming your controller sets @league to either a new (in the new
action) or
existing (in the edit action) instance of League. This should
automatically
generate the right url in the admin namespace to either post or put the
form
depending on wheter or not @league.new_record? is true.

Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

The only difference between the index action and the create action is
whether you’re doing a get (index) or post (create). You can always
make sure you are using the right method by adding a :method=>xxx to
the form_for.

On Jun 15, 11:19 am, Chris O. [email protected]

On Sun, Jun 15, 2008 at 11:19 AM, Chris O.
[email protected] wrote:

wheter or not @league.new_record? is true.

That way worked. The weird thing is that I re-tried the way I did it
the first time and this time it worked. Not sure why since I had
restarted the server a number of time when trying to figure it out last
night.

Could it be that your original example read:

<% form_for :league, :url => admin_leagues_url do |f| %>

instead of

<% form_for :league, :url => admin_league_url do |f| %>

(Notice the difference between “admin_leagues_url” (plural) and
“admin_league_url” (singular)).

–wpd

Rick Denatale wrote:

I believe that the preferred way to do this is

form_for([:admin, @league]) do |f|

Assuming your controller sets @league to either a new (in the new
action) or
existing (in the edit action) instance of League. This should
automatically
generate the right url in the admin namespace to either post or put the
form
depending on wheter or not @league.new_record? is true.

That way worked. The weird thing is that I re-tried the way I did it
the first time and this time it worked. Not sure why since I had
restarted the server a number of time when trying to figure it out last
night.

I was unaware that you could pass in an array to the form for to allow
the admin subdir to be added to the url, which is why I didn’t use that
method in the first place.

Thanks for the help.

The only difference between the index action and the create action is
whether you’re doing a get (index) or post (create). You can always
make sure you are using the right method by adding a :method=>xxx to
the form_for.

Ya, I tried that, but the initial form was rendering with the method =
“post” already.

Could it be that your original example read:

<% form_for :league, :url => admin_leagues_url do |f| %>

instead of

<% form_for :league, :url => admin_league_url do |f| %>

(Notice the difference between “admin_leagues_url” (plural) and
“admin_league_url” (singular)).

–wpd

It is the plural version that I want for a POST.