Passing json from rails to coffeescript

30 Views Asked by At

I'm trying to untangle some code I wrote a few years ago. I thought it was all working, but then I upgraded to Rails 7 and started getting some stranger errors on one page.

Working code:

maps_controller.rb

def grid
    @grids = Grid.select("id, geom, grid_name").where("ispub is true").order("id")
    feature_collection = Grid.to_feature_collection @grids
    @geojson = RGeo::GeoJSON.encode(feature_collection)

    respond_to do |format|
      format.json { render json: @geojson }
      format.html
    end
  end

grid.coffee

  $.ajax
    dataType: 'text'
    url: 'grid.json'
    success: (data) ->
      L.geoJSON(JSON.parse(data), style: gridStyle, onEachFeature: onEachFeature ).addTo map
    error: ->
      alert "Failed to load AJAX data"

This seems to pass a file called grid.json per the Developer Tools in Chrome Working screenshot

Non-working code

plants_controller.rb

  def map
    @pId = params[:id]
    if @pId.present?
      @plants = Plant
        .where(id: @pId)
      feature_collection = Plant.to_feature_collection @plants
    else
      @plants = Plant
        .order(:id)
        .eager_load(:genus, :type, :gis, :detail)
        .find_not_dead
        .find_grid
        .where(["types.type_name not LIKE ? and types.type_name not LIKE ? and gis.geom is not null", "A%", "B%"])
    end
    feature_collection = Plant.to_feature_collection @plants
    @geojson = RGeo::GeoJSON.encode(feature_collection)

    respond_to do |format|
      format.json { render json: @geojson }
      format.html
    end
  end

plants.coffee

  $.ajax
    dataType: 'text'
    url: map.json
    send: () ->
      $('#loading').show()
    complete: () ->
      $('#loading').hide()

This seems to create a file a maps (no .json extension) per Developer Tools in Chrome. Non-working screen shot

Any help understanding why the second one doesn't work would be wonderful. It's been a few years

0

There are 0 best solutions below