OK, this is another exercise in frustration. I cannot for the life of me figure out what's wrong.
Controller:
class MapController < ApplicationController
layout "map"
def index
if params.has_key? :grade
@students = Student.find(:grade => params[:grade])
@subjects = Subject.where(:grade => @students.map(&:grade))
else
@students = Student.all
@subjects = Subject.all
end
@json = @students.to_gmaps4rails do |student, marker|
marker.infowindow render_to_string(:partial => "/map/infowindow", :locals => {:student => student})
marker.title "#{student.name}"
marker.json({:address => student.full_address, :grade => student.grade, :subjects => student.subjects})
end
respond_to do |format|
format.html
format.xml {render :xml => @subjects.to_xml}
format.json {render :json => @subjects.to_json}
end
end
end
Coffeescript:
jQuery ->
$(".mapDataGrade").append "<option value=-1>-</option>"
$(".mapDataGrade").append "<option value=#{i}>#{i}</option>",
i for i in [8..12]
$(".mapDataSubject").append "<option value=-1>-</option>"
grade = "grade": "#{$('.mapDataGrade').val()}", tagmode: "any", format: "json"
$.getJSON "map.json",
grade,
(data)->
console.log(data)
$('.mapDataSubject').find('option').remove().end()
$(".mapDataSubject").append "<option value=-1>-</option>"
$.each data.items, (i,item) ->
$(".mapDataSubject").append "<option value=#{i}>#{i}</option>"
Now the first part is working perfectly fine, its the .getJson that isn't. At the first page load, it returns an internal server error (500) which is understandable because an id of -1 is being returned, but that still doesn't excuse why the console.log(data) is recording the student data and not the subject. This I find to be very puzzling. Also, why does the controller not see the key "grade" which is obviously being sent? Furthermore, is there another way (read better) to test for the existence of a key in the params hash? In a nutshell, what is wrong here so that this would actually work?
TIA