2011-02-03 10 views
0

我正在使用gMap(http://gmap.nurtext.de/)在Rails應用中顯示地圖數據。它的工作方式是傳遞包含變量的JSON標記,並將它們呈現在Google Map上。當你點擊一個標記時,它會顯示變量'html'中包含的HTML。使用gMap渲染正在傳遞的部分內容使用gMap

它工作正常,但此刻我從模型的JSON傳遞HTML。這讓我很煩,因爲這個html應該在視圖中。我想以某種方式傳遞JSON中的部分,然後在HTML中呈現。

在JSON是產生的力矩,在模型中,使用此代碼

def as_json(options={}) 

    hash = Hash.new 
    hash[:longitude] = longitude.to_i 
    hash[:latitude] = latitude.to_i 
    hash[:html] = "<p>#{title}</p>" 
    hash[:popup] = true 
    hash[:url] = "/listings/"+id.to_s 

    return hash  
end 

的:HTML關鍵是我想放像

hash[:html] = "<%= render :partial => "listings/bubble", :locals => {:listing => @listings.first} %>" 

的部分作品本身 - 我已經把它放在一個視圖來檢查。當你在JSON中有直線html時,彈出式泡泡也可以工作。

然而當部分是JSON內通過生成的代碼是

<div class="gmap_marker"> 
    <%= render :partial => "listings/bubble", :locals => {:listing => @listings.first} %> 
</div> 

它只是結束了作爲HTML字符串,它永遠不會呈現。

所以我想有兩個辦法我在尋找:

  1. 它是作爲JSON(例如,在模式結束) 或
  2. 局部呈現在傳遞前的部分呈現視圖被渲染。

不知道這是一個JavaScript問題還是Rails問題,但任何幫助將不勝感激!

乾杯

回答

0

您已經有了正確的想法,但你必須使用render_to_string功能:

hash[:html] = render_to_string(:partial => "listings/bubble", 
    :locals => {:listing => @listings.first}) 
+1

謝謝,但是這給了我以下錯誤: `NoMethodError在ListingsController#指數未定義的方法`render_to_string'for#<上市:0x1031cda00>` – Nick 2011-02-03 08:01:09

相關問題