如何将数据从javascript传递到rails模型

时间:2014-11-14 01:04:22

标签: ruby-on-rails model coffeescript relationship

我正在使用datatables gem来选择广告系列的某些位置。一旦做出选择,我希望将位置数组(即咖啡脚本)传递到广告系列模型中,以便我可以在正确的表格中建立正确的关系。

目前,我不知道将数据数组从咖啡脚本传递到广告系列模型的任何流程或方法。怎么办呢?

目前的咖啡脚本(数据在数组allLcoations中):

$("#showAllRestaurants").click ->

    allLocations = root.table.rows().data()

    $('#multi_markers').map ->
      handler = Gmaps.build("Google", builders: { Marker: CustomMarkerBuilder })
      handler.buildMap
        internal:
          id: "multi_markers"
      , ->
        for aLocation in allLocations
          markers = handler.addMarkers([
            {
              lat: aLocation[9]
              lng: aLocation[10]
              custom_marker:     "<img src='/assets/images/redDotMarker.png' width='40' height='40'>"
              custom_infowindow: "Store Number: #{aLocation[1]}; Address: #{aLocation[2]}, #{aLocation[3]}; Major Bidding City: #{aLocation[6]}"
            }
          ])

        handler.bounds.extendWith markers
        handler.fitMapToBounds()
        return

1 个答案:

答案 0 :(得分:0)

您可以直接在视图中编写coffeescript,也可以创建新文件。

如果您希望直接在视图中编写它。您所要做的就是在HAML代码之前插入:coffeescript。对于HTML,请插入<coffeescript> code ... </coffescript>

在HAML中

:coffeescript
  $("#showAllRestaurants").click ->

    allLocations = root.table.rows().data()

    $('#multi_markers').map ->
      handler = Gmaps.build("Google", builders: { Marker: CustomMarkerBuilder })
      handler.buildMap
        internal:
          id: "multi_markers"
      , ->
        for aLocation in allLocations
          markers = handler.addMarkers([
            {
              lat: aLocation[9]
              lng: aLocation[10]
              custom_marker:     "<img src='/assets/images/redDotMarker.png' width='40' height='40'>"
              custom_infowindow: "Store Number: #{aLocation[1]}; Address: #{aLocation[2]}, #{aLocation[3]}; Major Bidding City: #{aLocation[6]}"
            }
          ])

        handler.bounds.extendWith markers
        handler.fitMapToBounds()
        return

在HTML中

<coffeescript>
  $("#showAllRestaurants").click ->

      allLocations = root.table.rows().data()

      $('#multi_markers').map ->
        handler = Gmaps.build("Google", builders: { Marker: CustomMarkerBuilder })
        handler.buildMap
          internal:
            id: "multi_markers"
        , ->
          for aLocation in allLocations
            markers = handler.addMarkers([
              {
                lat: aLocation[9]
                lng: aLocation[10]
                custom_marker:     "<img src='/assets/images/redDotMarker.png' width='40' height='40'>"
                custom_infowindow: "Store Number: #{aLocation[1]}; Address: #{aLocation[2]}, #{aLocation[3]}; Major Bidding City: #{aLocation[6]}"
              }
            ])

          handler.bounds.extendWith markers
          handler.fitMapToBounds()
          return
</coffeescript>