Question

I have two examples that accomplish the same thing:

<% if @site.addresses %>
<% @site.addresses.each do |address| %>
<p> <%= address.primary_street + ' | ' if address.primary_street.present? -%>
    <%= address.secondary_street + ' | '  if address.secondary_street.present? -%>
    <%= address.city + ', ' if address.city.present? -%>
    <%= address.region -%>
    <%= ' ' + address.postal_code if address.postal_code.present? -%></p>
<% end %>
<% end %>

and

<% if @site.addresses %>
    <% @site.addresses.each do |address| %>
    <p><%= address.primary_street ? address.primary_street + ' | ' : nil -%>
        <%= address.secondary_street ? address.secondary_street + ' | '  : nil -%>
        <%= address.city ? address.city + ', ' : nil -%>
        <%= address.region ? address.region : nil -%>
        <%= address.postal_code ? ' ' + address.postal_code : nil -%></p>
    <% end %>
<% end %>

They both do exactly what I want, which is output formatted address data from the database. I will have some addresses that don't have street address, some that will have both primary and secondary, etc.

Is there a better, less repetitive way to do this... like the rails way?

For what it is worth this is a rails 4 app.

Was it helpful?

Solution

I suggest method is model.

in addresss.rb

def formatted_address
  fa = ""
  fa += primary_street    ? primary_street + ' | ' : ""
  fa += secondary_street  ? secondary_street + ' | '  : ""
  fa += city              ? city + ', ' : ""
  fa += region            ? region : ""
  fa += postal_code       ? ' ' + postal_code : ""
  fa
end

in view(you don't need to check @site.addresses isNil or isEmpty, because in this case not iterate)

<% @site.addresses.each do |address| %>
   <p><%= address.formatted_address %></p>
<% end %>

OTHER TIPS

So I found this solution to be more optimized. Some of my address database entries showed the formatting for the secondary street even though it was nil. A little research suggested the blank? test is a better solution than nil or true tests. This works like a charm. Thanks again for the help Taekmin!

def one_line_address 
    one_line_address = ""
    one_line_address += primary_street + ' | ' unless primary_street.blank?
    one_line_address += secondary_street + ' | ' unless secondary_street.blank?
    one_line_address += city 
    one_line_address += ', ' + region unless region.blank?
    one_line_address += ' ' + postal_code
    one_line_address
end

First. you used same naming for method and local variable 'one_line_address'.

It is very confusing coding style.

rails supports method checking nil or empty(=blank?).

one_line_address += secondary_street + ' | ' unless secondary_street.nil? && secondary_strret.empty?

simple version

one_line_address += secondary_street + ' | ' unless secondary_street?

simple version(some_field?) can be accepted only for columns of ActiveRecord.

English is not my first language so my english is cryptograph

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top