Check non-zero and not empty in Rails shortcut?

I have a display page for my users, and each attribute should only be visible on this page, if it is not zero, and not an empty string. Below I have my controller, and it’s rather annoying to write the same line of code @user.city != nil && @user.city != "" For each variable. I'm not very good at creating my own methods, but can I somehow create a shortcut to do something like this: @city = check_attr(@user.city) ? Or is there a better way to shorten this procedure?

users_controller.rb

 def show @city = @user.city != nil && @user.city != "" @state = @user.state != nil && @user.state != "" @bio = @user.bio != nil && @user.bio != "" @contact = @user.contact != nil && @user.contact != "" @twitter = @user.twitter != nil && @user.twitter != "" @mail = @user.mail != nil && @user.mail != "" end 
+49
ruby ruby-on-rails
Apr 09 '14 at 17:41
source share
2 answers

There is a way that does this for you:

 def show @city = @user.city.present? end 

present? method test present? for not- nil plus has content. Blank lines, lines consisting of spaces or tabs are considered absent.

Since this template is so common, there’s even a shortcut in ActiveRecord:

 def show @city = @user.city? end 

This is roughly equivalent.

As a side note, vs nil testing is almost always redundant. In Ruby, there are only two logically false values: nil and false . If it is not possible for the variable to be literal false , this would be sufficient:

 if (variable) # ... end 

This is preferable to the usual if (!variable.nil?) Or if (variable != nil) stuff, which sometimes appears. Ruby is leaning toward a more reductionist type of expression.

One of the reasons you want to compare vs. nil , is if you have a tri-state variable that can be true , false or nil , and you need to distinguish between the last two states.

+137
Apr 09 '14 at 17:44
source share

You can use . present? which is part of ActiveSupport.

 @city = @user.city.present? # etc ... 



You can even write it like

 def show %w(city state bio contact twitter mail).each do |attr| instance_variable_set "@#{attr}", @user[attr].present? end end 



It is worth noting that if you want to check that something is empty, can you use .blank? (is it the opposite of .present? )

Also, do not use foo == nil . Use foo.nil? .

+8
Apr 09 '14 at 17:43
source share



All Articles