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 ?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With