# Routing issue/bug?

**URL:** <https://www.ruby-forum.com/t/routing-issue-bug/140500>\
**Category:** Rails\
**Created:** [June 15, 2008, 6:00am UTC](https://www.ruby-forum.com/t/routing-issue-bug/140500 "2008-06-15T06:00:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [June 15, 2008, 6:00am UTC](https://www.ruby-forum.com/t/routing-issue-bug/140500/1 "2008-06-15T06:00:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [June 15, 2008, 6:45am UTC](https://www.ruby-forum.com/t/routing-issue-bug/140500/2 "2008-06-15T06:45:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [June 15, 2008, 6:49am UTC](https://www.ruby-forum.com/t/routing-issue-bug/140500/3 "2008-06-15T06:49:41Z")

</div>

# 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”}

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [June 15, 2008, 2:29pm UTC](https://www.ruby-forum.com/t/routing-issue-bug/140500/4 "2008-06-15T14:29:32Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [June 15, 2008, 3:05pm UTC](https://www.ruby-forum.com/t/routing-issue-bug/140500/5 "2008-06-15T15:05:59Z")

</div>

On Sun, Jun 15, 2008 at 12:00 AM, Chris O. \<  
removed\_email\_address@domain.invalid\> 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/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [June 16, 2008, 2:20pm UTC](https://www.ruby-forum.com/t/routing-issue-bug/140500/6 "2008-06-16T14:20:51Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [June 16, 2008, 2:33pm UTC](https://www.ruby-forum.com/t/routing-issue-bug/140500/7 "2008-06-16T14:33:54Z")

</div>

On Sun, Jun 15, 2008 at 11:19 AM, Chris O.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [June 15, 2008, 5:19pm UTC](https://www.ruby-forum.com/t/routing-issue-bug/140500/8 "2008-06-15T17:19:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [June 16, 2008, 3:40pm UTC](https://www.ruby-forum.com/t/routing-issue-bug/140500/9 "2008-06-16T15:40:31Z")

</div>

> 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.
