嵌套属性问题 - 回形针

时间:2011-08-16 21:58:30

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

3个模特

class Country < ActiveRecord::Base
    has_many :regions

    has_many :assets, :dependent => :destroy
    accepts_nested_attributes_for :assets
end

class Region < ActiveRecord::Base
    belongs_to :country
    has_many :appartments

    has_many :assets, :dependent => :destroy
    accepts_nested_attributes_for :assets
end

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

end

编辑表格

<%= f.fields_for :assets do |asset| %>

       <% if asset.object.new_record? %>
             <%= asset.file_field :image %>
       <% end %>

   <% end %>

我的区域控制员:

def edit
 @country = Country.find(params[:country_id])


# For URL like /countries/1/regions/2/edit

    @region = @country.regions.find(params[:id])
    5.times { @country.region.assets.build }
end

我得到了

"undefined method `region' for #<Country:0x007fbf8b848198>" 

有人的想法?

1 个答案:

答案 0 :(得分:0)

5.times { @country.region.assets.build }

应该是

5.times { @region.assets.build }