5

I have the following models set up:

class Contact < ActiveRecord::Base
  belongs_to :band
  belongs_to :mode

  validates_presence_of :call, :mode
  validates_associated :mode, :band
  validates_presence_of :band, :if => :no_freq?
  validates_presence_of :freq, :if => :no_band?

protected

  def no_freq?
    freq.nil?
  end

  def no_band?
    band.nil?
  end
end

class Band < ActiveRecord::Base
  has_many :logs
end

class Mode < ActiveRecord::Base
  has_many :logs
end

When I enter a frequency on my new view it allows for no band to be specified if a freq is entered. This creates a problem in my other views though because band is now nil. How do I allow for band not to be specified and just show up as empty on my index and show views, and then in the edit view allow one to be specified at a later point in time.

I have been able to get my index to display a blank by doing:

contact.band && contact.band.name

But I'm not sure if this is a best approach, and I'm unsure of how to apply a similar solution to my other views.

Many thanks from a rails newb!

Badweather
  • 261
  • 1
  • 3
  • 9

5 Answers5

9

In my views, I use the following for potentially nil objects in my views:

<%= @contact.band.name unless @contact.band.blank? %>

if your object is an array or hash, you can use the empty? function instead.

<%= unless @contacts.empty? %>
 ..some code
<% end %>

Hope this helps!

D

Dustin M.
  • 2,884
  • 3
  • 20
  • 18
  • Using unless with the .blank? method was just what I needed. Thanks for the help! – Badweather Apr 08 '10 at 15:50
  • 3
    You can also do `<%= @contact.band.name if @contact.band.present? %>` – John Topley Apr 08 '10 at 15:56
  • Your welcome badweather! Welcome to Stack Overflow btw and don't forget to accept the answer. (Shameless reputation grubbing) ;) And John is right, you can use an if method with "present?" Cheers! – Dustin M. Apr 08 '10 at 16:22
  • Answer accepted. :) Thanks John for that other suggestion as well. I will try both. – Badweather Apr 08 '10 at 17:43
3

A couple years old but still a top Google result for "rails view handle nil" so I'll add my suggestion for use with Rails 3.2.3 and Ruby 1.9.3p0.

In application_helper.rb, add this:

def blank_to_nbsp(value)
  value.blank? ? "&nbsp;".html_safe : value
end

Then to display a value in a view, write something like this:

<%= blank_to_nbsp contact.band %>

Benefits:

  • "blank" catches both nil values and empty strings (details).
  • Simply omitting a nil object, or using an empty string, may cause formatting issues. &nbsp; pushes a non-breaking space into the web page and preserves formatting.
  • With the "if" and "unless" suggestions in other answers, you have to type each object name twice. By using a helper, you only have to type each object name once.
Community
  • 1
  • 1
Mark Berry
  • 17,843
  • 4
  • 58
  • 88
2
<%= @contact.try(:band).try(:name) %>

This will return nil if band or name do not exist as methods on their respective objects.

Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
2

You can use Object#andand for this:

<%= @contact.band.andand.name %>
Christoffer Hammarström
  • 27,242
  • 4
  • 49
  • 58
0

<%= @contact.band if @contact.band %> also works

Lloyd Powell
  • 18,270
  • 17
  • 87
  • 123
fivetwentysix
  • 7,379
  • 9
  • 40
  • 58