Helpers -- Problem using one

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

Hi Curtis,

Great explanation!! Especially the alternative options!!

Thanks a lot. Richard

Curtis Summers wrote:

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.

2) 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: <tr> 11: <td> 12: <%= link_to_customer(c) %> 13: </td> 14: <td align="right"> <%= c.balance %> </td> 15: <td> <%= c.last_issue_dt %> </td>

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

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

Curtis Summers wrote: