2015-06-18 34 views
-2

在Ruby on Rails中,我試圖創建一個類似於digg.com的新聞彙總站點。當用戶點擊縮略圖時,我想要啓動源網站的URL。如何使用link_to image_tag從模型中的字段中檢索路徑?

現在我的代碼在創建「列表」的應用程序中打開URL。我在名爲「源」的「列表」中有一個字段,其中包含新聞源的URL。有人可以調整我的link_to image_tag,以便點擊縮略圖啓動「列表」中的「源」字段?

<% @listings.each do |listing| %> 

    <div class="col-md-4"> 

     <div class="thumbnail"> 

     <%= link_to image_tag(listing.image.url), listing %> 
+2

'<(%)=的link_to IMAGE_TAG(listing.image.url),listing.source,目標: 「_blank」 %>' –

+0

謝謝Pardeep。那讓我一半在那裏。如果我的「源代碼」是cnn.com,則生成的html將包含我的應用程序的路徑,然後在最後添加cnn.com。防爆。 http://tenacious-dragon-14-134879.use1-2.nitrousbox.com/cnn.com是否可以直接前往cnn.com。我的應用程序給出了路由錯誤,說沒有路由匹配[GET]「/cnn.com」 – Thunderpuppy

+0

感謝Pardeep。我明白了。我需要http://之前的URL,然後它處理它很好。我非常感謝你的幫助。 – Thunderpuppy

回答

1

這裏listing.source將回到你的網站的網址,你要移動和target: '_blank'將在新標籤中打開鏈接。

<%= link_to image_tag(listing.image.url), listing.source, target: "_blank" %> 
相關問題