Al D
Al D

Reputation: 667

Ruby display address with commas

Hi I have an organisation model that accepts nested attributes for an address model. Everything is working fine but when I try and display the address, the best I've come up with so far is:-

<%= "#{@organisation.address.line1} #{@organisation.address.line2} #{@organisation.address.line3} #{@organisation.address.line4} #{@organisation.address.postcode} #{@organisation.address.country}" %>

This works, but it doesn't add a comma between the fields. I can manually add a comma, but that looks silly if the address doesn't have a line4 etc.

Is there an easier way to do this so that the address displays like below and doesn't add a comma after a field if it's blank (and also adds a full stop after the last field)?

The address, the road, the postcode, the country.

Thanks for looking

Edit

Thanks for all the suggestions. I'm going to try create a helper method on the application controller that can be called from any view because the address model is used by multiple other models.

Upvotes: 2

Views: 154

Answers (5)

bschaeffer
bschaeffer

Reputation: 2904

[line_1, line_2, city, "#{state} #{zip}".strip].compact.join(', ')

Upvotes: 1

tagCincy
tagCincy

Reputation: 1599

@Vimsha answer is correct, however, I would go a step further and override the to_string method on the Address model:

def self.to_s
    [line1, line2, line3, line4, postcode, country].reject(&:blank?).join(", ")
end

Then you can easily call it on the view:

<%= @organization.address.to_s %>

EDIT: You want to override to_s, not to_string

Upvotes: 1

SHS
SHS

Reputation: 7744

For view-specific methods, consider using decorators. You add a layer of decorators and put view-specific methods there. Google "rails and decorators"

Moving on to how it should be done:

address = @organization.address
array = [address.line1, address.line2, address.city]
"#{array.join(', ')}." # also look into the "to_sentence" method

If you want to exclude nil entries, use compact before the join.

If you want to exclude blank entries, use

array.reject(&:blank?)

If you're looking to add entries in the array based on their presence, then the following would be suitable.

array = [].tap do |arr|
  arr << address.line1 if address.line1.present?
  # and so on
end

Upvotes: 1

usha
usha

Reputation: 29349

<%= [@organisation.address.line1, @organisation.address.line2, @organisation.address.line3, @organisation.address.line4, @organisation.address.postcode, @organisation.address.country].reject(&:blank?).join(", ") %>

It would be better to move it to a helper though

Upvotes: 2

Ruby Racer
Ruby Racer

Reputation: 5740

You can go like this:

<%= "#{@organisation.address.line1},#{@organisation.address.line2},\
     #{@organisation.address.line3},#{@organisation.address.line4},\
     #{@organisation.address.postcode},#{@organisation.address.country}".
     gsub(/,+/,",").gsub(/(\A,)|(,\Z)/,'')%>

This way you kill double commas and also any commas appearing at start or end of the string

Upvotes: 1

Related Questions