Topic: This code is pretty messy- is there any way to clean it up?

<h3><%= @customer.first_name %> <%= @customer.last_name %>
    <% if @customer.business_name != '' %>
        @ <%= @customer.business_name %>
    <% end %>
        </h3>
        <p>Phone Number: <br/><%= @customer.phone_number %></p>
    <% if @customer.cell_number %>
        <p>Cell: <br/><%= @customer.cell_number %></p>
    <% end %>
    <% if @customer.work_number %>
        <p>Work: <br/><%= @customer.work_number %></p>
    <% end %>
    <% if @customer.alt_number %>
        <p>Alt #: <br/><%= @customer.alt_number %></p>
    <% end %>

<p>email: <br/><%= @customer.email %></p>

<p>Address: <br/><%= @customer.street_address%>
<br/><%= @customer.city %>, <%= @customer.state %> <%= @customer.zip %></p>

Re: This code is pretty messy- is there any way to clean it up?

See if this helps:

## add some useful methods in the model ##
class Customer < ActiveRecord::Base
  ...
  def name_and_business
    name = "#{first_name} #{last_name}"
    return business_name.blank? ? name : name + "@ #{business_name}"
  end
  def city_state_zip
    "#{city}, #{state} #{zip}"
  end
  ...
end

## the view ##
<h3><%= @customer.name_and_business %></h3>
<% [{"Phone Number" => @customer.phone_number || "N/A"},
    {"Cell"         => @customer.cell_number},
    {"Work"         => @customer.work_number},
    {"Alt"          => @customer.alt_number},
    {"Email"        => @customer.email || "N/A"}].each do |k,v| %>
  <%= "<p>#{k}: <br/>#{v}</p>" if v %>
<% end  %>
<p>Address: <br/><%= @customer.street_address%>
<br/><%= @customer.city_state_zip %></p>

Re: This code is pretty messy- is there any way to clean it up?

Unfortunately I'm getting a NoMethodError with this code in place. It keeps showing this:

'undefined method `name_and_business' for #<Customer:0x47ac778>'

any ideas?

Re: This code is pretty messy- is there any way to clean it up?

That method needs to be in your customer model.  If you have that, then you probably need to restart your server.

Re: This code is pretty messy- is there any way to clean it up?

Thanks for the help! I put it in the computer model sad

Only one problem now... The phone numbers and emails aren't showing up.

Any suggestions (and mind if I get an explination of the code for that part, its a little new to me.)

Thanks again!

Re: This code is pretty messy- is there any way to clean it up?

Change the phone number and email to look like this:

{"Email"        => ( @customer.email || "N/A" ) }

That section of code is an array of hashes.  the array is used to hold order and the hashes are used to pair the name of the item with the value for that item.

The |k,v| means k will be what ever is on the left of the hash, the key. and v is the value, whatever is on the right.

The @customer.email || "N/A" means that if @customer.email is nil, then just return "N/A", otherwise use the email.

In the name_and_business method, you will see a tertiary command:
comparison ? true : false
So in our example we have:
business_name.blank? ? name : name + "@ #{business_name}"
which says if the business_name is nil or an empty string (blank?) then return just the name, otherwise if we have the business name return the name and the business name like this: name + "@ #{business_name}"

Hope that helps.

Re: This code is pretty messy- is there any way to clean it up?

Thanks for the help so far! I'm actually starting to understand now I think. Now I just have to get the array to work properly.

Even with the new email code in there its not working. I'll post the answer when I figure it out so that you can take a look at it.

Re: This code is pretty messy- is there any way to clean it up?

Still no answer over here... Any Ideas?

I still don't quite understand the whole hashes inside on an array thing, so I'll look into that some more.

Re: This code is pretty messy- is there any way to clean it up?

TechWraith wrote:

I'll post the answer when I figure it out so that you can take a look at it.

Thought you were working on it :)

Please be more specific about what is not working.  For example, what is it doing?  What are you expecting it to do?

Re: This code is pretty messy- is there any way to clean it up?

Sorry for not being more specific.

None of the information that is supposed to be displayed through the hashes and arrays are displaying.

Here's the code for displaying the Phone numbers and email:

<% [{"Phone Number" => @customer.phone_number},
    {"Cell"         => @customer.cell_number},
    {"Work"         => @customer.work_number},
    {"Alt"          => @customer.alt_number},
    {"Email"        => @customer.email}].each do |k,v| %>
  <%= "<p>#{k}: #{v}</p>" if v %>
<% end  %>

I took out the two N/A's just to see if that was the problem (it wasn't)

Thanks for your help so far!

Re: This code is pretty messy- is there any way to clean it up?

Ah - all wrong, sorry about that.
Corrected code:

[{"Phone Number" => @customer.phone_number},
{"Cell"         => @customer.cell_number},
{"Work"         => @customer.work_number},
{"Alt"          => @customer.alt_number},
{"Email"        => @customer.email}].each do |h| %>
  <%= "<p>#{h.keys[0]}: #{h.values[0]}</p>" if h.values[0] %>
<% end  %>

Re: This code is pretty messy- is there any way to clean it up?

Thanks! It works!