# Helpers: newbie problem

**URL:** <https://www.ruby-forum.com/t/helpers-newbie-problem/101147>\
**Category:** Rails\
**Created:** [August 3, 2006, 1:34pm UTC](https://www.ruby-forum.com/t/helpers-newbie-problem/101147 "2006-08-03T13:34:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vincentVega](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/8baadc/32.png) [@vincentVega](https://www.ruby-forum.com/u/vincentVega)\
**Post date:** [August 3, 2006, 1:34pm UTC](https://www.ruby-forum.com/t/helpers-newbie-problem/101147/1 "2006-08-03T13:34:02Z")

</div>

please help me with my code:

im calling a helper, which has to return different links.

–list.rhtml

…  
\<% for message in @messages %\>

 \<%= message.author.send('name') %\> \<%= message.send('date').strftime("%d.%m.%y/%H.%M.%S") %\> \<%= message.send('subject') %\> \<%= test(3,message) %\> #THE PROBLEM \<% end %\> ...

–application\_helper.rb

def test(level,message)  
if level == 2  
link\_to “read”, :action =\> “show”, :id =\> message.send(‘id’)  
elsif level == 3  
link\_to “read”, :action =\> “show”, :id =\> message.send(‘id’) #PROBLEM  
1  
link\_to “edit”, :action =\> “edit”, :id =\> message.send(‘id’) #PROBLEM  
2  
else  
end  
end

the problem is, when i call the test helper, with the param level 3, i  
become only one link, the last one and not both of them.

what can i do?? please help!  
thanks!

---

<div class="post-metadata">

**Author:** ![vincentVega](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/8baadc/32.png) [@vincentVega](https://www.ruby-forum.com/u/vincentVega)\
**Post date:** [August 3, 2006, 2:23pm UTC](https://www.ruby-forum.com/t/helpers-newbie-problem/101147/2 "2006-08-03T14:23:47Z")

</div>

On Thu, Aug 03, 2006 at 01:34:02PM +0200, vincentVega wrote:

> else  
> end  
> end
> 
> the problem is, when i call the test helper, with the param level 3, i  
> become only one link, the last one and not both of them.

Your method ‘test’ is returning a string for printing in your view. The  
string it’s returning is the value of the last expression evaluated –  
that  
is, the edit link\_to. What you want to do is mush it all up into a  
single  
expression with a +, and everything will work.

Oh, and the empty else clause will cause grown men to weep.

- Matt

–  
I’m not sure which upsets me more: that people are so unwilling to  
accept  
responsibility for their own actions, or that they are so eager to  
regulate  
everyone else’s.

---

<div class="post-metadata">

**Author:** ![vincentVega](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/8baadc/32.png) [@vincentVega](https://www.ruby-forum.com/u/vincentVega)\
**Post date:** [August 3, 2006, 2:27pm UTC](https://www.ruby-forum.com/t/helpers-newbie-problem/101147/3 "2006-08-03T14:27:49Z")

</div>

Hi vincent,

don’t know if this is the proper way to do things (I’m using partials  
most  
often) but you might try (untested):

def test(level, message)  
case level  
when 2  
return read\_link(message)  
when 3  
html = read\_link(message)  
html += ‘  
’  
html += link\_to ‘edit’, :action =\> ‘edit’, :id =\> message  
return html  
end # case  
end # def test

def read\_link(message)  
link\_to ‘read’, :action =\> ‘show’, :id =\> message # message should  
send id  
without the explicit call  
end

ruby returns the result of the last executed line of code. This is why  
only  
one link is returned…

Cheers  
Jan

---

<div class="post-metadata">

**Author:** ![vincentVega](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/8baadc/32.png) [@vincentVega](https://www.ruby-forum.com/u/vincentVega)\
**Post date:** [August 3, 2006, 3:30pm UTC](https://www.ruby-forum.com/t/helpers-newbie-problem/101147/4 "2006-08-03T15:30:58Z")

</div>

vincentVega \<wizarduo@…\> writes:

> \<%= message.author.send('name') %\> if level == 2 the problem is, when i call the test helper, with the param level 3, i become only one link, the last one and not both of them.
> 
> what can i do?? please help!  
> thanks!

Hi,

Ruby method’s return value is the last statement executed in the method.  
That’s why you get only the last line and not both the lines.

So your code may be modified as

–list.rhtml  
\<% for message in messages %\>

 \<%= message.author.send('name') %\> \<%= message.send('date').strftime("%d.%m.%y/%H.%M.%S") %\> \<%= message.send('subject') %\> \<% if somecondition ==3 %\> \<%= test(3,message) %\> \<%= test(4,message) %\> \<%end%\> \<% end %\>

–application\_helper.rb

def test(level,message)  
if level == 2  
link\_to “read”, :action =\> “show”, :id =\> message.send(‘id’)  
elsif level == 3  
link\_to “read”, :action =\> “show”, :id =\> message.send(‘id’)  
elsif level == 4  
link\_to “edit”, :action =\> “edit”, :id =\> message.send(‘id’)  
end  
end

HTH,  
Ashutosh
