【问题标题】:Rails 3 rendering partial with javascript from remote: true callRails 3 从远程使用 javascript 渲染部分:真正的调用
【发布时间】:2012-06-29 09:46:00
【问题描述】:

我的 Rails 3 应用控制器 HotelsController 中有一个 remote: true 调用,它执行 show_map 操作。很简单。

<%= link_to "Map", show_hotel_map_path(@hotel.id), remote: true %>
<div id="hotel_map"></div>

show_hotel_map 操作非常基本

def show_hotel_map
  @hotel = Hotel.find(params[:id])
  respond_to do |format|
    format.html
    format.js
  end
end

show_hotel_map.js.erb 用于在 JQuery-UI 对话框中渲染部分 map,并带有渲染的 Google Maps 地图

$("#hotel_map").dialog({
  autoOpen: true,
  height: 'auto',
  width: 'auto',
  modal: true,
  closeOnEscape: true,
  position: 'center',
  resizable: false,
  title: "Map",
  open: function(){
      $("#hotel_map").html("<%= escape_javascript(render partial: "map") %>")
  }
});

_map.html.erb partial 什么都不做,只是执行render_map helper

<%= render_map %>

render_map helper 完成了使用Gmaps4Rails gem 渲染地图的所有工作

module HotelsHelper

  def render_map
    if @hotel.address.latitude && @hotel.address.longitude
      map = @hotel.address.to_gmaps4rails
    else
      geo = Gmaps4rails.geocode("#{@hotel.continent.name}, #{@hotel.country.name}, #{@hotel.location.name}")[0]
      map = [{lat: geo[:lat], lng: geo[:lng]}].to_json
    end
    gmaps(:markers => { :data => map, :options => { :draggable => true } })
  end
end

问题是Gmaps4Rails gem 渲染了一些 javascript 以便正确处理 Google 地图,但显然这个 javascript 被来自show_map.js.erb 视图的escape_javascript 调用截断。因此,当我单击"Map" 链接时,正在呈现 JQuery-UI 对话框,但它没有任何有效负载,因为没有执行任何 Javascript。 render_map helper 本身就可以完美地工作,所以我想这不是Gmaps4Rails gem 的问题。是否有任何解决方法,以便我可以执行该javascript?我能想到一些 hack,但也许有更多 Rail-ish 方法来做到这一点?

谢谢!

【问题讨论】:

    标签: ruby-on-rails-3 gmaps4rails


    【解决方案1】:

    好吧,它可能关心的人,我使用 Gmaps4Rails wiki 中描述的 UJS 技术解决了它

    https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Using-with-UJS

    它不是很干燥,但我想没有太多其他选择。

    【讨论】:

    猜你喜欢
    • 2013-03-15
    • 1970-01-01
    • 2012-07-20
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多