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 Eclipse 3.1.2
Ruby 1.8.2-15 Rails 1.1.4 Java JDK 1.5.0_06
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:
- Mix-in the customer helper methods into your other controller with
‘helper’:
helper CustomerHelper
- Move your link_to_customer definition to application_helper.rb so it
will be available to all controllers/views.
Hi Curtis,
Great explanation!! Especially the alternative options!!
Thanks a lot.
Richard
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
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
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.
- 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