以下代码在我的视图中显示所有精选事件及其所有相关图像。 我想知道如何只展示每个特色活动的第一张图片。
# event model
class Event < ActiveRecord::Base
attr_accessible :title, :start_date, :end_date, :content, :is_featured, :assets_attributes
has_many :assets, :order => 'asset_order ASC'
accepts_nested_attributes_for :assets, :allow_destroy => true
end
# asset model
class Asset < ActiveRecord::Base
belongs_to :event
belongs_to :static
has_attached_file :asset, :styles => { :large => "660x270#", :medium => "300x300#", :thumb => "100x100#" }
end
# event model
class Event < ActiveRecord::Base
attr_accessible :title, :start_date, :end_date, :content, :is_featured, :assets_attributes
has_many :assets, :order => 'asset_order ASC'
accepts_nested_attributes_for :assets, :allow_destroy => true
end
# static controller
def show
@events = Event.where(:is_featured => 1).includes(:assets)
@static = Static.where(:id => params[:id]).first
...
end
# static show view
- @events.each do |event|
- event.assets.each do |asset|
=image_tag asset.asset.url(:medium)
= event.title
= event.start_date.to_date
答案 0 :(得分:0)
你在找这样的东西吗?
# static show view
- @events.each_with_index do |event, index|
- event.assets.each do |asset|
=image_tag asset.asset.url(:medium) unless index > 0
= event.title
= event.start_date.to_date