Stuck on something that should be so darn simple - link_to a "foo_bar" controller method?

Man, as a newbie as soon as I deviate just an inch from the hello world stuff I'm in all sorts of hurt:)...

I have a controller (lets call it GroceryStoreController) that has an 'add' method (It can be foobar for all I care at the moment, just something that isn't the conventional ones produced from scaffolding and that you get mappings to automatically from 'resources' definition in Routes.)

I now have a list of grocery store items and I want to provide an 'add' link next to each item. Hitting the add link should reach the "add" method in the controller (passing along the id of the grocery item.)

This should be so simple, yet I can't seem to get anything to work. I don't think I can just use my own xyz_path in link_to since I think that's just a convention for the built in resources (edit ,etc.) eg:

link_to 'add', add_grocery_store_path(item) #causes undefined path error

I even tried doing things a way that docs say was and older way?:

//sorry pasting from real code I was working with (results after you do a search)... not grocery_store, but same idea...

@meters.each do |m| ....     link_to 'add', controller => "meters_search", :action => "add", :id => m

The above creates a link but it looks really bad:

<a href="/meters_search/add?%23%3CMetersSearchController%3A0x00000102e90c48%3E=meters_search&amp;id=1">add</a>

and when you click on it it's trying to find the 'show' method (which I don't have in this controller, for now just have index, and add until I get this working) so it breaks.

Maybe it's my Routes that is a problem, and I've been trying different things there as well. Relevant routes for this example I have as:

resources :meters_search get "meters_search/add" => 'meters_search#add'

[ side not for any help in my learning: I'm also am trying to following the docs for link_to

and it's extremely confusing. It shows some options but then also mentions see options available by looking at "url_for" so when I click on that link ActionView::Helpers::UrlHelper I see seven options listed on the page ( anchor, only_path, etc.) yet they keep showing examples of options like :action, :controller yet I don't see them shown anywhere in the API as valid options? ]

Man, as a newbie as soon as I deviate just an inch from the hello world stuff I'm in all sorts of hurt:)...

I have a controller (lets call it GroceryStoreController) that has an 'add' method (It can be foobar for all I care at the moment, just something that isn't the conventional ones produced from scaffolding and that you get mappings to automatically from 'resources' definition in Routes.)

I now have a list of grocery store items and I want to provide an 'add' link next to each item. Hitting the add link should reach the "add" method in the controller (passing along the id of the grocery item.)

This should be so simple, yet I can't seem to get anything to work. I don't think I can just use my own xyz_path in link_to since I think that's just a convention for the built in resources (edit ,etc.) eg:

Look into the "member" option...

map.resource :grocery_stores, :member => {:add => :get}

-philip

Thanks Philip! That was the key.

After I added:

resources :meters_search do   member do       get 'add'   end end

I was able to use

link_to 'add', add_meters_search_path(m)

There's a punctuation error in your pasted example. I'll let you find it.

Next time, show us the actual code you are working with.