Thanks in advance for any help you can provide!
For my Ruby on Rails website (1.8.7, 2.3.15) I’m trying to pull data from a MySQL table, but I’m running into an “Undefined Method” error. My goal with this code is to display a text list of all locations that fall within a certain latitude and longitudinal range.
Production.log:
ActionView::TemplateError (undefined method `each' for nil:NilClass) on line #31 of app/views/its/map.html.erb:
28: <!-- TestingLocations -->
29:
30: <ul id="locations">
31: <% for masterlocation in @nearbylocations %>
32: <li><%= masterlocation.nickname %></a></li>
33: <% end %>
34: </ul>
Map controller:
def map
@its = Its.find(params[:id])
if @its.user_id == current_user.id
@locations = Location.find(:all, :conditions => ["its_id = ?", params[:id]], :order => "order_num asc")
@mapscount = Saved.count(:all, :conditions => ['its_id = ?', params[:id]])
#@date_filter = Date.civil(params[:date_filter].values_at(:year, :month, :day))
# debugger
respond_to do |format|
format.html # map.html.erb
format.xml { render :xml => @its }
end
else
redirect_to '/'
@nearbylocations = Masterlocation.find(:all, :conditions => ["latitude > 25 AND latitude < 30 AND longitude > -75 AND longitude < -70", params[:id]], :order => ['nickname asc'])
end
Again, I appreciate your help!
This happens because
@nearbylocationsisnilwhere as it is expecting anArrayyou can avoid this by using
I also notice that you are declared instance variable after redirecting.
Ideally
should be
But after redirection you can’t use
@nearbylocationsso you have to use eitherrenderor declare it in redirected method.