2012-07-29 25 views
0

我有一個帶jQuery內容輪播的城市頁面。傳送帶的內容由每個循環提交。每個循環中的動態url_path

CityController 
    @events = @city.events.find_all_by_hot(true) 
    @activities = @city.activities.find_all_by_hot(true) 
    @sights = @city.sights.find_all_by_hot(true) 
    @hot = @events + @activities + @sights 

Class city 
    has_many: events 
end 

class events 
    belongs_to :city 
    has_many :attachments, :as => :attachable  
    accepts_nested_attributes_for :attachments 
end 

活動和景點車型是相同的

City view content slider: 
    @hot.each do |a| 
    a.attachments.each do |a| 
    = image_tag(a.file.url, :height =>"325px", :width =>"650px"), url_path 

我要生成在我的每個循環鏈接(url_path)...我怎麼能認識到這一點?它不能放置路徑的url_path,因爲它們是基於哪個附件(圖像)加載的dymanic。

回答

1

雖然你的image_tag語法不正確,你可以試試這個

@hot.each do |hot| 
    hot.attachments.each do |a| 
    link_to polymorphic_path(a.attachable) do 
     image_tag(a.file.url, :height => "325px", :width => "650px") 
    end 
    end 
end 

如果我正確理解你的問題。還請查看polymorphic_path幫手,這正是您所需要的。

0

我說對了,鏈接應該指向a哪個可以是任何事件,活動,景點?作爲

@hot = @events + @activities + @sights 

我會嘗試建立在CityController

def hottie 
    @duck = Kernel.const_get(params[:type]).find_by_id(params[:id]) 
    redirect_to @duck 
    end 

一個特殊的控制器動作,然後添加像

match 'hottie/:type/:id' => 'city#hottie', as: 'hot' 

東西應該給你,你可以爲這個使用路徑幫手:

<%=link_to("Open", hot_path(a.class.to_s, a.id)) %> 

此外:這當然有點髒,需要考慮一些安全因素(例如,限制它只顯示特殊類型)。您也可以考慮使用STI將三個類Event,Activities和Sights移動到對象層次結構中;這應該消除在請求中傳遞類型的需要。