Refactoring advice

Hi Rcommunity!

small refactoring question here. This is code:

def login_block
ret = “”
ret += “


ret +="#{current_user_full_name}"
ret += “|
ret += image_tag(“logout.png”)
ret += “#{link_to t(‘logout’),’#’,:id =>
‘fb_logout’}

ret += “
  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

" #{current_user_full_name}" ...... EOS

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

On Tue, Apr 12, 2011 at 8:48 PM, Burebista [email protected] wrote:

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

‘fb_logout’}"
ret += “”

 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.

[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

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