Prettier code in views for chained values?

Sometimes in a view I have written wretchedly, ugly code like this:

<%= if @person and and and %>

I’m calling this a chained value. These values can run deep whether
they are the part of an ActiveRecord association (e.g. a child record)
or a simple composite field (e.g. address_city and address_state become and address.state).

In any case, we have to make sure everything exists before we try to
display a deeply chained value. Otherwise, our app barfs.

Before I go reinventing the wheel, I am wondering how others may have
handled this, perhaps with a generic helper method. I am considering
writing one. I would want it to be pretty to look at, maybe something
along these lines:

<%= chained_value %>

Trouble is, I don’t think this will work because I am guessing it will
attempt to resolve the value it intends on passing. I could describe
the chain and handle it with an eval, I guess, but it looks less pretty:

<%= chained_value “” %>

The method definition might look something like this:

def chained_value(value_chain, no_value = “”, error_value = “N/A”)

It has a value it displays when the last item in the chain is blank, and
one it displays if it chokes while trying to work it’s way down the

Your thoughts?
Mario T. Lanza


Mario T. Lanza wrote:

Sometimes in a view I have written wretchedly, ugly code like this:

<%= if @person and and and %>

you can try

<%= rescue nil %>

That way, if everything’s fine you get the value or else you get


javier ramírez

Thanks, Javier! I knew there had to be a better way.

I did, however, go ahead and solve the problem with this:

def chained_value(no_value = “”, error_value = “N/A”)
value = yield
value.blank? ? no_value : value

alias :cv :chained_value

This allows for one extra thing: a substitute value to use when blank.

<%= cv(“No City”, “No Company Address”){} %>

Plus it’s just as concise:

<%= rescue nil %>
<%= cv{} %>


def chained_value(no_value = “”, error_value = “N/A”)
yield || no_value rescue error_value


<%= cv(“No City”, “No Company Address”){} %>


<% || “No City” rescue “No Company Address”

Just experimenting with the syntax.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs