开发者

display first result in included model, RoR3

The following code displays all featured events, and all associated images of these, inside my view. I'd like to know how I can display just the first image of each featured event.

# event model
class Event < ActiveRecord::Base
  attr_accessible :title, :start_date, :end_date, :content, :is_featured, :assets_attributes
  has_many :assets, :order => 'asset_o开发者_如何学Gorder 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


Are you looking for something like this?

# 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜