Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if locals are defined in a partial?

What is the correct syntax to test if locals have been defined and passed to a partial?

For example, I render a partial

<%= render partial: "data-map", locals: {lat: @model.lat, lon: @model.lon, zoom: 10} %>

And in the partial need to do something like

<%= map( options: {
  latitude: if lat.defined? ? lat : 0,
  longitude: if lon.defined? ? lon : 0,
  zoom: if zoom.defined? ? zoom : 50
  }
%>

I'm having trouble getting this working.

I also saw the following in the API

<% if local_assigns.has_key? :headline %>
  Headline: <%= headline %>
<% end %>

but am having trouble with this as well. Perhaps I'm not getting the syntax correct.

Grateful for any pointers

like image 833
Andy Harvey Avatar asked Nov 04 '22 22:11

Andy Harvey


1 Answers

Drop the if from the ternary format as follows:

<%= map( options: {
  latitude: defined?(lat) ? lat : 0,
  longitude: defined?(lon) ? lon : 0,
  zoom: defined?(zoom) ? zoom : 50
  }
%>
like image 156
Anil Avatar answered Nov 09 '22 10:11

Anil