1

我正在建立一个名为的模型Wall,其中有很多Assets. 资产模型是回形针模型。一切似乎都很好,但它不会呈现表单。在walls controllerI am building 5 assets with the line5.times { @wall.assets.build }中。该行在ap @wall.assets.build控制台中很好地吐出 5 条资产记录。我已经包含了下面的代码。我看不出我做错了什么。有人知道吗?

墙.rb

class Wall < ActiveRecord::Base
  has_many :assets, :as => :assetable
  accepts_nested_attributes_for :assets, :allow_destroy => true
end

资产.rb

class Asset < ActiveRecord::Base
  belongs_to :assetable, :polymorphic => true
  attr_accessible :assetable_id, :assetable_type
  has_attached_file :asset, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end

wall_controller.rb

  def new
    @wall = Wall.new
    5.times do
      @wall.assets.build
    end
    ap @wall.assets
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @wall }
    end
  end

  def edit
    @wall = Wall.find(params[:id])
    5.times do
      @wall.assets.build
    end
  end

_form.html.haml

= form_for @wall, :html => { :class => 'form-horizontal', :multipart => true } do |f|
  .control-group
    = f.label :name, :class => 'control-label'
    .controls
      = f.text_field :name, :class => 'text_field'
  - f.fields_for :assets do |asset|
    = asset.file_field :asset
  .form-actions
    = f.submit nil, :class => 'btn btn-primary'
    = link_to t('.cancel', :default => t("helpers.links.cancel")), walls_path, :class => 'btn'

干杯

托尼

4

0 回答 0