# Helpers -- Problem using one

**URL:** <https://www.ruby-forum.com/t/helpers-problem-using-one/68611>\
**Category:** Rails\
**Created:** [September 13, 2006, 10:20pm UTC](https://www.ruby-forum.com/t/helpers-problem-using-one/68611 "2006-09-13T22:20:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rjlyders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/58f4c7/32.png) [@rjlyders](https://www.ruby-forum.com/u/rjlyders)\
**Post date:** [September 13, 2006, 10:20pm UTC](https://www.ruby-forum.com/t/helpers-problem-using-one/68611/1 "2006-09-13T22:20:25Z")

</div>

Hi All,

I’ve got a view “app\views\home\start…rhtml” that invokes a partial  
template with:

\< %= render :partial =\> “customer/list” % \>

(spaces inserted between % and its associated angle bracket here and in  
the  
following code).

The partial view “app\views\customer\_list.rhtml” creates a link with  
the  
code:

\< %= link\_to(c.name,

:controller =\> ‘customer’,

:action =\> ‘show’,

:id =\> c.id)

% \>

I’d like to replace the code in the partial view with a one-line helper:

\< %= link\_to\_customer(c) % \>

and define the helper in the module in “app\helpers\customer\_helper.rb”  
with:

def link\_to\_customer(c)

link\_to(c.name,

```
  controller => 'customer',

  action => 'show',

  id => c.id)

```

end

When I tried this, I got:

undefined method `link\_to\_customer’ for #\<#Class:0x3cb2710:0x3cb2278\>

Any ideas of a way to get this going?

Thanks in advance,

Richard

* * *

Env.: WinXP-Pro/SP2, MS [VisualStudio.NET](http://VisualStudio.NET) Eclipse 3.1.2  
Ruby 1.8.2-15 Rails 1.1.4 Java JDK 1.5.0\_06

---

<div class="post-metadata">

**Author:** ![rjlyders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/58f4c7/32.png) [@rjlyders](https://www.ruby-forum.com/u/rjlyders)\
**Post date:** [September 13, 2006, 10:36pm UTC](https://www.ruby-forum.com/t/helpers-problem-using-one/68611/2 "2006-09-13T22:36:25Z")

</div>

Richard L. wrote:

> I’ve got a view “app\views\home\start…rhtml” that invokes a partial  
> template with:  
> \< %= render :partial =\> “customer/list” % \>  
> undefined method `link\_to\_customer’ for #\<#Class:0x3cb2710:0x3cb2278\>

I’m assuming you are calling this partial while in the home controller  
(or a controller other than the customer controller). Your  
link\_to\_customer helper method is not mixed into the home controller,  
and is therefore not available. You have some choices:

1. Mix-in the customer helper methods into your other controller with  
‘helper’:

helper CustomerHelper

1. Move your link\_to\_customer definition to application\_helper.rb so it  
will be available to all controllers/views.

---

<div class="post-metadata">

**Author:** ![rjlyders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/58f4c7/32.png) [@rjlyders](https://www.ruby-forum.com/u/rjlyders)\
**Post date:** [September 13, 2006, 10:54pm UTC](https://www.ruby-forum.com/t/helpers-problem-using-one/68611/3 "2006-09-13T22:54:29Z")

</div>

Hi Curtis,

Great explanation!! Especially the alternative options!!

Thanks a lot.  
Richard

---

<div class="post-metadata">

**Author:** ![rjlyders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/58f4c7/32.png) [@rjlyders](https://www.ruby-forum.com/u/rjlyders)\
**Post date:** [September 13, 2006, 11:46pm UTC](https://www.ruby-forum.com/t/helpers-problem-using-one/68611/4 "2006-09-13T23:46:05Z")

</div>

Richard wrote:

> Hi Curtis,
> 
> Woops. I’m not out of the woods yet.

Your link\_to usage from above was:

def link\_to\_customer©  
link\_to(c.name,  
controller =\> ‘customer’,  
action =\> ‘show’,  
id =\> c.id)  
end

but should be:

def link\_to\_customer©  
link\_to(c.name, {:controller =\> ‘customer’,  
:action =\> ‘show’,  
:id =\> c.id})  
end

---

<div class="post-metadata">

**Author:** ![rjlyders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/58f4c7/32.png) [@rjlyders](https://www.ruby-forum.com/u/rjlyders)\
**Post date:** [September 14, 2006, 3:52am UTC](https://www.ruby-forum.com/t/helpers-problem-using-one/68611/5 "2006-09-14T03:52:58Z")

</div>

Hi Curtis,

Thanks again. My excuse (we always need one when we screw up, don’t  
we), is that I’m a Ruby and Rails newbie. So I’m insufficiently  
sensitive to those pesky little colons.

Incidentally, I copied the code I started with from somewhere (but  
missed the colons), and that source didn’t use any braces to form the  
hash. So I left them out to see whether it would work without them,  
wish it did.

I infer from this success that the link\_to method’s prototype requires  
a hash and the Ruby interpreter accepts the hash-like elements as  
components of the needed hash. Just a theory. No need to spend any more  
time on me.

Again, thank you very much for pushing me along.

Best wishes,  
Richard

---

<div class="post-metadata">

**Author:** ![rjlyders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/58f4c7/32.png) [@rjlyders](https://www.ruby-forum.com/u/rjlyders)\
**Post date:** [September 13, 2006, 11:28pm UTC](https://www.ruby-forum.com/t/helpers-problem-using-one/68611/6 "2006-09-13T23:28:33Z")

</div>

Hi Curtis,

Woops. I’m not out of the woods yet.

> I’m assuming you are calling this partial while in the home controller  
> Right.

> 1. Move your link\_to\_customer definition to application\_helper.rb so it  
> will be available to all controllers/views.  
> Chose this option, definition same as before.

Substituted my helper invocation in app\views\customer\_list.rhtml  
(line 12) and got error messages (looks like it’s looking for ‘show’  
in home rather than customer folder):

NameError in Home#start

Showing app/views/customer/\_list.rhtml where line #12 raised:

undefined local variable or method `action’ for  
#\<#Class:0x3cd7520:0x3cd7418\>

Extracted source (around line #12):

9: \<% @customers.each do |c| %\>  
10:

  
11:   
12: \<%= link\_to\_customer© %\>  
13:   
14: \<%= c.balance %\>   
15: \<%= c.last\_issue\_dt %\> 

Maybe I have to shift to the other option. I thought I’d wait to see  
if you had an approach to make the chosen option work. Maybe it’s not  
worth wasting your time on it.

Best wishes,  
Richard
