Tutorial :how to use to_sentence for links?



Question:

some thing like this:

John send a message to user1, user2, user3 and user4

this does not work

@users.collect{ |u| link_to(u.name, user_path(u)) }.to_sentence  


Solution:1

ez,

link_to in Erector goes right to the output stream. You either need to replace to_sentence, patch into link_to's behavior, or replace link_to. As link_to in this context is simple, I'd recommend that:

  rawtext users.map { |u|        "<a href='#{user_path(u)}'>#{u.name}</a>"    }.to_sentence  


Solution:2

Odd...

@users.collect{ |u| link_to(u.name, user_path(u)) }.to_sentence  

and

@users.map{ |u| link_to(u.name, user_path(u)) }.to_sentence  

Should work. What error are you getting?


Solution:3

Make sure you are actually printing out the results with <%= ... %>, I know I sometimes forget the equal sign and spend a lot of time trying to figure things out.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »