Sharethefun
Sharethefun

Reputation: 824

Rails way to check for object existence before display

I want to find a nice way to check my objects before i display them in the view so I wont get errors.

This is my controler

 @user = User.find_by_username(params[:username])
 @profile = @user.profile 
 @questions = @user.questions

and this is my view

 <% unless @profile.blank? %><%= link_to 'Edit Profile', :controller => 'profiles', :action => 'edit' %><% end %>


     <% unless @user.blank? %>
        Username:<%= @user.username %><br />
    Member Since:<%= @user.created_at.strftime("%d %B %Y")  %><br />
    <% end %>

  <% unless @profile.blank? %>
    First Name: <%= @profile.first_name %><br />
    Last Name: <%= @profile.last_name %><br /><br />
    About: <%= @profile.body %><br /><br />
    Location: <%= @profile.location %><br />
    Birthday: <%= @profile.birthday.strftime("%d %B %Y") %><br />
    <% end %>

As you can see, I'm using more than one checking of each kind ( check unless @profile.blank? ) and I think there will be a better way to do that.

Is there any Rails way to do something smarter than the one I've come up with ?

Upvotes: 5

Views: 20478

Answers (5)

ankit.vic
ankit.vic

Reputation: 101

Probably the rails approach would be to use Object#presence

you should always unless @object.blank? as you did.

Upvotes: 0

Daniel Morales
Daniel Morales

Reputation: 13

According to the rails documentation, you can see if any associated objects exist by using the association.nil? method:

if @book.author.nil?
  @msg = "No author found for this book"
end

Upvotes: 1

jibiel
jibiel

Reputation: 8313

Also

<%= if @object.present? %>

looks much simplier for me than

<%= unless @object.blank? %>

especially when we have more than one conditional statement (&& \ || \ and \ or).

api.rubyonrails.org

Upvotes: 10

sameera207
sameera207

Reputation: 16629

As I see, there is no way you can skip this @.blank? validation as you dont want to display the records if they are empty but I have few suggestions

1 - Make following sections as partials

<% unless @user.blank? %>
    Username:<%= @user.username %><br />
    Member Since:<%= @user.created_at.strftime("%d %B %Y")  %><br />
<% end %>

and

<% unless @profile.blank? %>
  First Name: <%= @profile.first_name %><br />
  Last Name: <%= @profile.last_name %><br /><br />
  About: <%= @profile.body %><br /><br />
  Location: <%= @profile.location %><br />
  Birthday: <%= @profile.birthday.strftime("%d %B %Y") %><br />
<% end %>

it will keep your view cleaner and will give you the flexibility of using them in anyware in your app

2 - take the below line

<% unless @profile.blank? %><%= link_to 'Edit Profile', :controller => 'profiles', :action=> 'edit' %><% end %>

inside the profile display as its more appropriate

cheers

sameera

Upvotes: 6

Eimantas
Eimantas

Reputation: 49354

How about building empty profile for user before saving it? How about using analogy with exclamation mark which will raise ActiveRecord::RecordNotFound which in turn will display 404 page.

P.S. I'd also recommend trimming controller down to one instance variable.

Upvotes: 0

Related Questions