# Stuck on something that should be so darn simple - link\_to a "foo\_bar" controller method?

**URL:** <https://www.ruby-forum.com/t/stuck-on-something-that-should-be-so-darn-simple-link-to-a-foo-bar-controller-method/194612>\
**Category:** Rails\
**Created:** [September 8, 2010, 12:47am UTC](https://www.ruby-forum.com/t/stuck-on-something-that-should-be-so-darn-simple-link-to-a-foo-bar-controller-method/194612 "2010-09-08T00:47:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rick\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@Rick\_R](https://www.ruby-forum.com/u/Rick_R)\
**Post date:** [September 8, 2010, 12:47am UTC](https://www.ruby-forum.com/t/stuck-on-something-that-should-be-so-darn-simple-link-to-a-foo-bar-controller-method/194612/1 "2010-09-08T00:47:15Z")

</div>

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:

[add](https://www.ruby-forum.com/meters_search/add?%23%3CMetersSearchController%3A0x00000102e90c48%3E=meters_search&id=1)

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

> **[ActionView::Helpers::UrlHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to)**
>
> Provides a set of methods for making links and getting URLs that depend on the routing subsystem (see ActionDispatch::Routing).

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](https://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-url_for)**
>
> Provides a set of methods for making links and getting URLs that depend on the routing subsystem (see ActionDispatch::Routing).

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? ]

---

<div class="post-metadata">

**Author:** ![Rick\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@Rick\_R](https://www.ruby-forum.com/u/Rick_R)\
**Post date:** [September 8, 2010, 1:51am UTC](https://www.ruby-forum.com/t/stuck-on-something-that-should-be-so-darn-simple-link-to-a-foo-bar-controller-method/194612/2 "2010-09-08T01:51:05Z")

</div>

On Tue, Sep 7, 2010 at 6:54 PM, Philip H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

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

–  
Rick

---

<div class="post-metadata">

**Author:** ![Rick\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@Rick\_R](https://www.ruby-forum.com/u/Rick_R)\
**Post date:** [September 8, 2010, 12:59am UTC](https://www.ruby-forum.com/t/stuck-on-something-that-should-be-so-darn-simple-link-to-a-foo-bar-controller-method/194612/3 "2010-09-08T00:59:32Z")

</div>

On Sep 7, 2010, at 3:46 PM, Rick R wrote:

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

---

<div class="post-metadata">

**Author:** ![Rick\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@Rick\_R](https://www.ruby-forum.com/u/Rick_R)\
**Post date:** [September 8, 2010, 3:16am UTC](https://www.ruby-forum.com/t/stuck-on-something-that-should-be-so-darn-simple-link-to-a-foo-bar-controller-method/194612/4 "2010-09-08T03:16:31Z")

</div>

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.
