refactoring advice

Hi Rcommunity!

small refactoring question here. This is code:

def login_block       ret = ""       ret += "<div class='right wrapper'>"       ret +="<strong>#{current_user_full_name}</strong>"       ret += "<span>|</span>"       ret += image_tag("logout.png")       ret += "<span>#{link_to t('logout'),'#',:id => 'fb_logout'}</span>"       ret += "</div>"

      ret.html_safe end

is there a way to get rid of those ugly ret += ?

Use a partial?

Here’s one way to refactor it using an array and content_tag.

content = [ content_tag(:strong, current_user_full_name), content_tag(:span, “|”), image_tag(“logout.png”), content_tag(:span, link_to(t(‘logout’), ‘#’, :id => ‘fb_logout’)) ]

return content_tag(:div, content.join(“\n”), :class => “right wrapper”).html_safe

ret = <<EOS <div class='right wrapper'>" <strong>#{current_user_full_name}</strong>" ...... EOS

Thank you! Finally I can see real world example of heredocs!

Hi Rcommunity!

small refactoring question here. This is code:

def login_block

  ret = ""
 ret += "<div class='right wrapper'>"

  ret +="<strong>#{current_user_full_name}</strong>"

  ret += "<span>|</span>"

  ret += image_tag("logout.png")

  ret += "<span>#{link_to t('logout'),'#',:id =>

‘fb_logout’}"

  ret += "</div>"



  ret.html_safe

end

if you’re using rails3, you can use a content_tag with a block

content_tag :div, {:class => ‘right wrapper’}, false do

     content_tag(:strong, current_user_full_name) +
     content_tag(:span, '|') +

image_tag(“logout.png”) + content_tag(:span, link_to(t(‘logout’),‘#’,:id => ‘fb_logout’)

end

content_tag accepts a boolean last attribute if you want to set

the content as html_safe.