please help me with my code: im calling a helper, which has to return different links. --list.rhtml ... <% for message in @messages %> <tr> <td><%= message.author.send(''name'') %></td> <td><%= message.send(''date'').strftime("%d.%m.%y/%H.%M.%S") %></td> <td><%= message.send(''subject'') %></td> <td><%= test(3,message) %></td> #THE PROBLEM <tr> <% 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! -- Posted via http://www.ruby-forum.com/.
On Thu, Aug 03, 2006 at 01:34:02PM +0200, vincentVega wrote:> --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.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 += ''<br/>'' 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060803/15de26a6/attachment.html
vincentVega <wizarduo@...> writes:> > please help me with my code: > > im calling a helper, which has to return different links. > > --list.rhtml > > ... > <% for message in <at> messages %> > <tr> > <td><%= message.author.send(''name'') %></td> > <td><%= message.send(''date'').strftime("%d.%m.%y/%H.%M.%S") %></td> > <td><%= message.send(''subject'') %></td> > <td><%= test(3,message) %></td> #THE PROBLEM > <tr> > <% 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! >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 <at> messages %> <tr> <td><%= message.author.send(''name'') %></td> <td><%= message.send(''date'').strftime("%d.%m.%y/%H.%M.%S") %></td> <td><%= message.send(''subject'') %></td> <% if somecondition ==3 %> <td><%= test(3,message) %></td> <td><%= test(4,message) %></td> <%end%> <tr> <% 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