# Url\_for , controller routing error

**URL:** <https://www.ruby-forum.com/t/url-for-controller-routing-error/67718>\
**Category:** Rails\
**Created:** [September 6, 2006, 10:23am UTC](https://www.ruby-forum.com/t/url-for-controller-routing-error/67718 "2006-09-06T10:23:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [September 6, 2006, 10:23am UTC](https://www.ruby-forum.com/t/url-for-controller-routing-error/67718/1 "2006-09-06T10:23:06Z")

</div>

I have an ‘account\_controller.rb’ in an ‘admin’ module  
I list the accounts with : [http://0.0.0.0:3000/admin/accounts/list](http://0.0.0.0:3000/admin/accounts/list),  
and get my table

trying to sort it, I use the follwoing sort\_link\_helper :

```
def sort_link_helper(text, param)
  key = param
  key += "_reverse" if @params[:sort] == param
  options = {
      :url => {:action => 'list', :params => @params.merge({:sort => 

```

key, :page =\> nil})},  
:update =\> ‘table’,  
:before =\> “Element.show(‘spinner’)”,  
:success =\> “Element.hide(‘spinner’)”  
}  
html\_options = {  
:title =\> “Sort by this field”,  
:href =\> url\_for(:action =\> ‘list’, :params =\>  
@params.merge({:sort =\> key, :page =\> nil}))  
}  
link\_to\_remote(text, options, html\_options)  
end

but the url\_for generates the href:  
[http://0.0.0.0:3000/admin/admin/accounts/list?sort=login](http://0.0.0.0:3000/admin/admin/accounts/list?sort=login)  
even if I try to insert a :controller =\> ‘/admin/account’ , nothing  
change  
@params : “{"action"=\>"list", "controller"=\>"admin/accounts"}”  
which seems correct, but the href is not… \<a  
href=“/admin/admin/accounts/list?sort=login”

I believe my problem is in the routing map, I have presently only  
map.connect ‘:controller/:action/:id’, :controller =\> ‘user’, :action  
=\> ‘welcome’

maybe I should have a map identifying the admin module… but I don’t  
know how to write it ?

thanks for any help

kad

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [September 6, 2006, 4:31pm UTC](https://www.ruby-forum.com/t/url-for-controller-routing-error/67718/2 "2006-09-06T16:31:49Z")

</div>

> even if I try to insert a :controller =\> ‘/admin/account’ , nothing  
> change  
> @params : “{“action”=\>“list”, “controller”=\>“admin/accounts”}”  
> which seems correct, but the href is not… \<a  
> href="/admin/admin/accounts/list?sort=login"

From the @params line above, it looks like you did not specify the  
leading slash (even though your comment on the line above has it). I  
think you need it.

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [October 6, 2006, 6:39pm UTC](https://www.ruby-forum.com/t/url-for-controller-routing-error/67718/3 "2006-10-06T18:39:09Z")

</div>

Kad K. wrote:

> I have an ‘account\_controller.rb’ in an ‘admin’ module  
> I list the accounts with : [http://0.0.0.0:3000/admin/accounts/list](http://0.0.0.0:3000/admin/accounts/list),  
> and get my table

> but the url\_for generates the href:  
> [http://0.0.0.0:3000/admin/admin/accounts/list?sort=login](http://0.0.0.0:3000/admin/admin/accounts/list?sort=login)

I’m getting this same behavior. Anybody have an idea for a fix or at  
least what’s causing it?

Thanks

ats

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [October 6, 2006, 8:14pm UTC](https://www.ruby-forum.com/t/url-for-controller-routing-error/67718/4 "2006-10-06T20:14:54Z")

</div>

Kad K. wrote:

> trying to sort it, I use the follwoing sort\_link\_helper :
> 
> ```
> def sort_link_helper(text, param)
> key = param
> key += "_reverse" if @params[:sort] == param
> options = {
> :url => {:action => 'list', :params => @params.merge({:sort => 
> 
> ```
> 
> key, :page =\> nil})},  
> :update =\> ‘table’,  
> :before =\> “Element.show(‘spinner’)”,  
> :success =\> “Element.hide(‘spinner’)”  
> }  
> html\_options = {  
> :title =\> “Sort by this field”,  
> :href =\> url\_for(:action =\> ‘list’, :params =\>  
> @params.merge({:sort =\> key, :page =\> nil}))  
> }  
> link\_to\_remote(text, options, html\_options)  
> end
> 
> but the url\_for generates the href:  
> [http://0.0.0.0:3000/admin/admin/accounts/list?sort=login](http://0.0.0.0:3000/admin/admin/accounts/list?sort=login)  
> even if I try to insert a :controller =\> ‘/admin/account’ , nothing  
> change  
> @params : “{"action"=\>"list", "controller"=\>"admin/accounts"}”  
> which seems correct, but the href is not… \<a  
> href=“/admin/admin/accounts/list?sort=login”

I’ve created a work around about the problem. It’s a kludge, but is  
seems to work.  
The params.merge(…) is in there so you keep the values of the  
parameters from previous requests. The problems is that :controller is  
set and the value causes problems. It works fine if modules aren’t  
involved, but the added members of the controller path cause problems  
when using modules.

Two options to fix:

1. Get rid of the params.merge call and expicitly add a :value =\>  
params[:value] statement to the array for all properties you want to  
save.

or

1. Clear out the :controller in the array passed to params.merge(…,  
:controller =\> nil)

This is probably a bug in url\_for, but these workaround should work.

ats
