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 @nearbylocations
is nil
where as it is expecting an Array
you can avoid this by using
<% for masterlocation in @nearbylocations %>
<li><%= masterlocation.nickname %></a></li>
<% end if @nearbylocations %>
I also notice that you are declared instance variable after redirecting. Ideally
redirect_to '/'
@nearbylocations = Masterlocation.find(:all, :conditions => ["latitude > 25 AND latitude < 30 AND longitude > -75 AND longitude < -70", params[:id]], :order => ['nickname asc'])
should be
@nearbylocations = Masterlocation.find(:all, :conditions => ["latitude > 25 AND latitude < 30 AND longitude > -75 AND longitude < -70", params[:id]], :order => ['nickname asc'])
redirect_to '/'
But after redirection you can't use @nearbylocations
so you have to use either render
or declare it in redirected method.
ur getting a nill error cause ur @nearbylocations isnt getting evaluated since the 'if' condition is executed and the Masterlocation query is in 'else'. u may move it out of the condition if the @nearbylocation is always needed.
the devil is here
:conditions => ["latitude > 25 AND latitude < 30 AND longitude > -75 AND longitude < -70", params[:id]]
this query gives nothing
you have @nearbylocations = nil
and than you receive undefined method
error
what in :conditions doing params[:id]
? i guess you forgot to delete it or you forgot to add ?
to query in ""
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.