How to Check If an Object Is Nil in a View in Ruby

How to check if an object is nil in a view in Ruby?

How about:

<% if !@foo.nil? && !@foo.new_record? %>
Hello!
<% end %>

First off, you need to be using AND logic rather than OR logic here, since any ActiveRecord object meets at least one the requirements of "not nil" or "not a new record".

Second, checking for nil first ensures that the second check isn't run if the first one fails. The error is thrown because you can't use #new_record? on an object that doesn't support it, so checking for nil first ensures that that method is never run on a nil.

Checking for nil in view in Ruby on Rails

Your example code remade:

controller code. ( I assume this is ItemsController )

def show
# This will fail with 404 if item is not found
# You can config rails to pretty much render anything on Error 404
@item = Item.find(params[:id])

# doesn't seem to be used in the view
# @folders = Folder.find(:all, :order => 'display_order')

# this is not needed anymore, or should be in the Error 404 handler
#if @item == nil or @item.folder == nil
# redirect_to(root_url) and return
#end
end

view code, since the controller made sure we have @item

#display the item's attributes here

<%= item_folder_link(@item) %>

helper code:

# display link if the item has a folder
def item_folder_link(item)
# I assume folder.name should be a non-blank string
# You should properly validate this in folder model
link_to( item.folder.name, folder_path(item.folder) ) if item.folder
end

Anyway, I try to keep view very very simple. Usually if I see loops and conditionals in views, I try to refactor them into helpers.

How to efficiently check if a value passed to a view is nil or empty

Use @cart.blank? to check whether it is nil or empty. The blank? method is a rails extension.

Ruby: check if object is nil

You seem to have a few questions here, so I'll take a stab at what seems to be the main one:

If you want to see if something is nil, just use .nil? - so in your example, you can just say request.nil?, which returns true if it is nil and false otherwise.

How I can check if an object is null in ruby on rails 2?

it's nilin Ruby, not null. And it's enough to say if @objectname to test whether it's not nil. And no then. You can find more on if syntax here:

http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Control_Structures#if

rails if object.nil? then magic '' in views?

What about:

<%= tax_payment.user.name if tax_payment.user %>

What is a better way to check for a nil object before calling a method on it?

Personally, I would use the or operator/keyword:

(financial_document.assets or []).length

Either way, .length is called on an array, giving you 0 if nil.

Rails Model method check if field nil, if so return empty string

You can, although this might not be what you wanted, simply remove any nil objects from the address Array using the #compact method, so that #empty? will work...:

def full_address
[self.address1, self.address2, self.address3,
self.city, self.state, self.zipcode].compact.reject!(&:empty?).join(', ')
end

You should be aware that this WILL return a partial address if SOME of the fields exist.

I would probably void any fault addresses (in this example I require just the first address line and the city fields):

def full_address
return "" unless self.address1 && self.city && !self.address1.empty? && !self.city.empty?
[self.address1, self.address2, self.address3,
self.city, self.state, self.zipcode].compact.reject!(&:empty?).join(', ')
end

Disregarding the few edits I made to the code, I would probably go with @Joseph's #present? together with the validation, since you're using Rails:

def full_address
return "" unless self.address1 && self.city && self.address1.present? && self.city.present?
[self.address1, self.address2, self.address3,
self.city, self.state, self.zipcode].reject!(&:present?).join(', ')
end

Checking for nil strings in Rails view

The idiomatic Ruby way to accomplish this is the || operator, which will return the value of the right-hand expression if the left-hand expression is nil (or false):

puts(user.photo.url || '')

Any moderately experienced Ruby programmer will understand exactly what that does. If you write a custom _? method, I now have to go look up the purpose of that method and remember what it does and hope that it always does the right thing. I generally find that sticking to idiomatic code is far more beneficial than saving a few keystrokes here and there.

Rails object update - check if any attribute is nil

To check if any of the object's attributes are nil, you can do

@object.attributes.values.include?(nil)

or

@object.attributes.values.any? &:nil?


Related Topics



Leave a reply



Submit