2012-07-29 64 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移动到对象层次结构中;这应该消除在请求中传递类型的需要。