Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6956301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:51:05+00:00 2026-05-27T14:51:05+00:00

I have this helper method that I can’t seem to clean up with content_tag

  • 0

I have this helper method that I can’t seem to clean up with content_tag in Rails 3.1

def site_address(site)
    html = '<address>'    
    html += site.address1 if site.address1.present?
    html += tag(:br) + site.address2 if site.address2.present?
    html += tag(:br) + site.city + ', ' if site.city.present?
    html += site.state.statecode if site.state.present?
    html += ' ' + site.zipcode if site.zipcode.present?
    html += '</address>'

    html.html_safe
end

If I try using content_tag(:address) do.... and then put the content in the block, it just escapes my <br /> tags.

Also notice there is a lot of if.present? logic because the table can have a bunch of null values.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:51:06+00:00Added an answer on May 27, 2026 at 2:51 pm

    Have you considered moving this logic into a partial? Also, you should be using the new #presence method.

    First, the method:

    def site_address(site)
      address = [
        site.address1.presence,
        site.address2.presence
      ].compact
    
      location = ""
      location << site.city << ", " if site.city.present?
      location << site.state.statecode if site.state.present?
      location << site.zipcode if site.zipcode.present?
    
      render :partial => "shared/site_address",
             :locals => { :address_lines => address, :location => location }
    end
    

    Then, the partial:

    <address>
      <% address_lines.each do |line| %>
        <%= line %><br>
      <% end %>
      <%= location %>
    </address>
    

    In general, using html_safe in a helper is a hint that you might be going overboard with HTML logic in your helper, and that it might a good idea to fall back on the template engine, which lets you more easily mix static and dynamic content with good default XSS-safe semantics.

    Note: If address1, address2, etc. are actually nil, and not possibly an empty String (I suspect this is true about site.state, at least), you don’t need to use present at all. Just say if site.state and call it a day. The present and presence methods simply treat empty values as if they were nil for the purpose of conditionals.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rails helper in my application_helper.rb file that looks like this: def
I have a helper method that creates navigation links for some controllers. def gen_associations(controllers)
I have this helper that I am building: def myhelper(object, attributes = []) attributes.each
Is there a better way to do this? I have an HTML helper extension
I want to create a helper method that can take in ANY type of
So I have a helper method that looks something like the following: private D
Assuming I have a decorator and a wrapped function like this: def squared(method): def
can i call helper functions inside controller classes? let's say i have this controller
I have a helper method that serialises an object, which works until you try
I have a Scala helper method that currently tries to fetch a URL and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.