New controller actions and url_for


#1

Hi,

I just defined a new controller action “list” for one of my
controllers, let’s call it “species”. To routes.rb, I added the
following entry:

map.resource :species, :member => { :list => :get }

I have a helper method in which I use url_for as follows:

url_for(:controller => ‘species’, :action => ‘list’, :params =>
params.merge({:sort => key, :page => nil}))

When I call this helper method from the view, it completely bypasses
the action in the generated URL. I get a URL as follows:

http://myserver.mydomain.org:8080/species?sort=sortparam

What I expect to get is a URL as follows:

http://myserver.mydomain.org:8080/species/list?sort=sortparam

I did a rake routes | grep list and I do get the following two
lines:

list_species GET /species/
list
{:controller=>“species”, :action=>“list”}
formatted_list_species GET /species/
list.:format
{:controller=>“species”, :action=>“list”}

Could someone please point out what needs to be corrected for url_for
to work properly? Is url_for not finding a route for
controller=species and action=list or is there some other problem?

Many thanks!
Amrita


#2

What you want is a collection, not a member:

map.resource :species, :collection => { :list => :get }

Also, instead of using url_for, you could use list_species_path( :sort
=> key, :page => nil )

On Wed, Oct 8, 2008 at 8:40 PM, amrita removed_email_address@domain.invalid wrote:

http://myserver.mydomain.org:8080/species/list?sort=sortparam

Could someone please point out what needs to be corrected for url_for
to work properly? Is url_for not finding a route for
controller=species and action=list or is there some other problem?

Many thanks!
Amrita


Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/
(en)
João Pessoa, PB, +55 83 8867-7208


#3

Thank you!

On Oct 8, 6:56 pm, “Maurício Linhares” removed_email_address@domain.invalid