3

提前感谢您在此问题上提供的任何帮助:如何使用 Ruby on Rails 从我的 MySQL 表“位置”中的“纬度”和“经度”数据创建 JSON 对象?

我的最终目标是创建一个谷歌地图,其中包含从 MySQL 数据库中提取的多个标记。我知道我可以通过 PHP 和 Google 教程 ( https://developers.google.com/maps/articles/phpsqlsearch_v3 ) 做到这一点,但我的 PHP 并不强,我正在寻找一种更短的方法来做到这一点导轨。

我已经尝试过这个其他教程(http://mokisystemsblog.blogspot.com/2013/04/add-markers-to-google-map-with-ruby-on.html),这是我的控制器代码:

    class MapallController < ApplicationController
      # GET /mapall
      # GET /mapall.xml
      # GET /mapall.js

      def index
        respond_to do |format|
          format.html do
            @locations = Location.find(:all)
          end
          format.xml  { render :xml => @locations }
          format.js do
            ne = params[:ne].split(',').collect{|e|e.to_f}  
            sw = params[:sw].split(',').collect{|e|e.to_f}
            @locations = Location.find(:all, :limit => 100, :bounds => [sw, ne])
            render :json => @locations.to_json
          end
        end
      end
    end

但是,当我访问 www.example.com/mapall.js 时,我得到一个错误代码。我希望这个链接能给我数据库中的完整结果集。

再次感谢您在学习此过程时的建议和耐心!

编辑 1 - 错误代码

以下是我访问 example.com/mapall、example.com/mapall.js 和 example.com/mapall.xml 时发生的情况的日志。当我访问 example.com/mapall 时,我希望有一个谷歌地图,它会从 MySQL 数据库中呈现我的所有位置。除此之外,我希望在访问 mapall.js 时能看到纬度/经度数据。从下面的日志中,我猜我得到 404 的原因是我的路由文件中没有到 mapall.js 的路由。是创建路由文件的路由的解决方案,如果是,应该如何读取?

再次感谢您的帮助!

    Processing MapallController#index (for IP at DATE TIME) [GET]
    Rendering template within layouts/mapall
    Rendering mapall/index
    Completed in 779ms (View: 7, DB: 100) | 200 OK [http://example.com/mapall]

    Processing ApplicationController#index (for IP at DATE TIME) [GET]

    ActionController::RoutingError (No route matches "/mapall.js" with {:method=>:get}):
    /phusion_passenger ERROR INFO
    Rendering /home/example/web/current/public/404.html (404 Not Found)

    Processing ApplicationController#index (for IP at DATE TIME) [GET]

    ActionController::RoutingError (No route matches "/mapall.xml" with {:method=>:get}):
    /phusion_passenger ERROR INFO
    Rendering /home/example/web/current/public/404.html (404 Not Found)

编辑 2 - 新控制器

感谢@kyllo 的反馈,我已经能够更新控制器,以便我可以将所有位置数据显示在http://example.com/mapall.js上。只剩下一步:只显示昵称、纬度和经度字段。下面显示了所有数据。我应该如何更改它以仅显示昵称、纬度和经度字段?

    class MapallController < ApplicationController
    # GET /mapall
    # GET /mapall.xml
    # GET /mapall.js

    def index
    respond_to do |format|
@masterlocation = Masterlocation.find(:all)
    format.html do
    end
    format.xml  { render :xml => @masterlocation }
    format.js do
    @masterlocation = Masterlocation.find(:all)
    render :json => @masterlocation.to_json
    end
    end
    end
    end
4

1 回答 1

0

我在这里看到了一些问题。

是的,您收到“无路由匹配”错误,因此您需要在 routes.rb 中设置与此类似的路由,以便您可以:format作为参数访问以响应:

match 'mapall.:format'

然后在您的控制器中,您有以下内容:

def index
    respond_to do |format|
      format.html do
        @locations = Location.find(:all)
      end
      format.xml  { render :xml => @locations }
      format.js do
        ne = params[:ne].split(',').collect{|e|e.to_f}  
        sw = params[:sw].split(',').collect{|e|e.to_f}
        @locations = Location.find(:all, :limit => 100, :bounds => [sw, ne])
        render :json => @locations.to_json
      end
    end
  end

@locations = Location.find(:all)需要在format.html do块之外,否则 @locations 在您的format.xml块内未定义,您将收到错误消息。

format.js do will allow your user to access the JSON API by visiting http:/www.example.com/mapall.js. If you use format.json do then they can access it at http:/www.example.com/mapall.json. The choice is yours.

Hope that helps to put you on the right track!

于 2013-06-27T15:45:53.527 回答