嵌套属性回形针

时间:2011-08-16 20:43:17

标签: ruby-on-rails ruby-on-rails-3

使用回形针查看我的图片。

型号:

class Country < ActiveRecord::Base
has_many :regions
has_many :assets, :dependent => :destroy
accepts_nested_attributes_for :assets
end

class Asset < ActiveRecord::Base
belongs_to :country
has_attached_file :image,
    :styles => {
      :thumb=> "100x100>",
      :small  => "300x300>",
      :large => "600x600>"
        }   
end

我的国家/地区index.html如下所示:

countries.each do |country| 
  country.name

我试过了:

link_to( image_tag(country.asset.image.url(:thumb)), country.asset.image.url(:original) )

但是我收到了错误。

有人的想法,我做错了什么?

1 个答案:

答案 0 :(得分:2)

每个国家/地区都有很多资产

countries.each do |country| 
  country.name
  country.assets.each do |asset|
    link_to( image_tag(asset.image.url(:thumb)), asset.image.url(:original) )

或将其更改为has_one关联