Helpers: newbie problem

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!

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.

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

vincentVega <[email protected]…> 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs