51224bdd17878b3b19e8987e9bb336a2

Below is the small part of my RHTML page.

- I am just showing the mobile number with carrier on user profile page.
- users table has mobile_no and carrier_id(foreign key)as columns .
- If user doesn't have his/her mobile no then in users table mobile_no field will be null and carrier_id will be 0. On profile page I am just keeping blank.
- Following is the code that I am using .
Can it be re factor.

Thanks in advance

<li style="width:190px" id="mobile_no_div">
		 <% if @user.carrier_id.nil? or @user.carrier_id == 0 %>  	
			 &nbsp;
		 <% else -%>
			<%=@user.carrier.name%>&nbsp;<%=@user.mobile_no%>
		 <% end -%>
</li>

Refactorings

No refactoring yet !

0333c8c993f63d263c9bc59ad2c35a9b

chalkers

June 12, 2008, June 12, 2008 10:20, permalink

1 rating. Login to rate!
<li style="width:190px" id="mobile_no_div">
			<%=@user.carrier.name + " " + @user.mobile_no if !@user.carrier_id.nil? && @user.carrier_id > 0  -%>
</li>
4f5d54213efb6502baab202f3fb2f09e

Simon

June 12, 2008, June 12, 2008 13:41, permalink

1 rating. Login to rate!

i think this is okay.

why draw an empty list item?

<%= '<li style="width:190px" id="mobile_no_div">' << @user.carrier.name << " " << @user.mobile_no << "</li>" unless @user.carrier_id.nil? && @user.carrier_id == 0 -%>
4f5d54213efb6502baab202f3fb2f09e

Simon

June 12, 2008, June 12, 2008 13:42, permalink

1 rating. Login to rate!

i made a typo lol && should be ||

<%= '<li style="width:190px" id="mobile_no_div">' << @user.carrier.name << " " << @user.mobile_no << "</li>" unless @user.carrier_id.nil? || @user.carrier_id == 0 -%>
880cbab435f00197613c9cc2065b4f5a

danielharan

June 12, 2008, June 12, 2008 15:47, permalink

1 rating. Login to rate!

having carrier_id == 0 in the view smells. what does that mean? Is there a mysql default that's polluting data?

<% if @user.carrier? %>
  <li style="width:190px" id="mobile_no_div">
    <%= @user.carrier.name %> &nbsp; <%= @user.mobile_no %>
  </li>
<% end %>
D41d8cd98f00b204e9800998ecf8427e

RickDumminic

June 14, 2008, June 14, 2008 04:53, permalink

1 rating. Login to rate!
<li style="width:190px" id="mobile_no_div">
  <%= @user.mobile_no && @user.carrier && @user.carrier.name && (@user.carrier.name + " " + @user.mobile_no) || "&nbsp;" %>
</li>
D41d8cd98f00b204e9800998ecf8427e

RickDumminic

June 14, 2008, June 14, 2008 04:55, permalink

1 rating. Login to rate!

Woops, missed ()

<li style="width:190px" id="mobile_no_div">
  <%= (@user.mobile_no && @user.carrier && @user.carrier.name && (@user.carrier.name + " " + @user.mobile_no)) || "&nbsp;" %>
</li>
51224bdd17878b3b19e8987e9bb336a2

DG

June 16, 2008, June 16, 2008 12:45, permalink

No rating. Login to rate!

Hay Thanks Guys for you codes.. :)

Your refactoring





Format Copy from initial code

or Cancel