2012-09-16 57 views
5

我想使用Twitter Bootstrap彈出窗口來顯示用戶頭像和電子郵件地址,並有可能在以後添加更多詳細信息。Rails - 在Twitter Bootstrap彈出窗口中渲染部分內容

我有一個問題得到部分呈現在鏈接的內容字段內。視圖代碼如下(在HAML中)。

%span.comment-username 
    =link_to comment.user_name, "#", "title" => comment.user_name, 
     "data-content" => "=render 'users/name_popover'", 
     class: "comment-user-name" 

目前,這隻會產生一個字符串的內容代碼。

有沒有辦法做到這一點,部分將被插入而不是作爲一個字符串的代碼?

回答

10

您希望rails實際評估字符串的內容,而不是僅顯示字符串。最簡單的方法是:

%span.comment-username 
    =link_to comment.user_name, "#", "title" => comment.user_name, 
    "data-content" => "#{render 'users/name_popover'}", 
     class: "comment-user-name" 

這應該將渲染語句傳遞給rails並呈現您的部分預期。

+0

謝謝 - 有點瞎搞後得出了上述結論。希望能幫助其他人。 – jmc

+0

這對我不起作用。我得到了逃脫的HTML。有什麼建議麼? –

+0

@KyleDecot是否嘗試過:html =>'true'選項? – anpsmn

1

感謝您的答案jrc - 它幫助我找到了解決方案。

我的解決方案可能是其他非HAML人跟我一樣有用:

`<%= link_to('Service History' , '#', :class => "popover-history", :rel => "popover", :"data-placement" => "bottom", :title => "Service History", :"data-content" => "#{render 'services/service_history'}") %>` 

`$(function() { 
$('.popover-history').popover({ html : true }); 
});` 
+0

我想在Rails 4.2.2視圖上顯示地圖。我想使用partials代替直接的html代碼,以防萬一我想更改popover窗口的格式。該解決方案回答了我的問題 –