1

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

mu is too short
  • 426,620
  • 70
  • 833
  • 800
SteveMustafa
  • 621
  • 2
  • 8
  • 19

1 Answers1

0

Part of your problem is right here:

format.json {render :json => @subjects.to_json}

When you say obj.to_json, you get a String back, a String that contains a JSON representation of obj but a String nonetheless. So, when asked for map.json, the server will end up doing this:

render :json => some_string_that_is_already_json

and you'll end up sending double-encoded data back to the client. The $.getJSON call is expecting a JSON string – not a JSON encoded JSON string – so I wouldn't expect anything sensible to happen. Your console.log(data) will show you something that looks like a JavaScript object but looks can be deceiving: a JSON string will look an awful lot like an object in the console.

As far as why your controller isn't getting :grade, have a look at what's in param, it should be immediately available in your logs.

And params.has_key? is the right way to see if params has a key, the alternatives have issues.

Community
  • 1
  • 1
mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • Started GET "/map.json?grade=11" for 127.0.0.1 at 2012-12-20 12:01:50 -0700 Processing by MapController#index as JSON Parameters: {"grade"=>"11"} Completed 500 Internal Server Error in 193ms ArgumentError (Unknown key: grade): app/controllers/map_controller.rb:6:in `index' – SteveMustafa Dec 20 '12 at 19:02
  • For some strange reason, I could not add another line to that comment. But the above is a straight copy and paster from the terminal log. grade is there, but I have no idea what's going on to stop it from seeing it. – SteveMustafa Dec 20 '12 at 19:03
  • Probably easier to add that to the question, comments don't deal with such things that well. – mu is too short Dec 20 '12 at 19:27
  • That ArgumentError sounds like Student doesn't have a `:grade`. – mu is too short Dec 20 '12 at 20:22