Rails:Polymorphic association和accepts_nested_attributes

时间:2014-08-07 15:55:01

标签: ruby-on-rails ruby-on-rails-4 rails-activerecord nested-attributes polymorphic-associations

附件不会保存。我在这里缺少什么? 在我的应用程序中,我有一个项目,每个项目用户都可以上传许多资产。上传由载波完成。

这是模型

class Project < ActiveRecord::Base

   has_many :assets,:as => :assetable,dependent: :destroy
   accepts_nested_attributes_for :assets, :allow_destroy => true

end


class Asset < ActiveRecord::Base

  belongs_to :project
  belongs_to :user
  belongs_to :assetable, :polymorphic => true
  mount_uploader :attachment, AttachmentUploader #carrierwave
  validates :attachment, presence: true
  validates :project_id, presence: true
end

这些是我的project_controller中的动作

  def new
     @project = Project.new
     @asset = @project.assets.build
   end 



  def create
      @project = Project.new(project_params)
      @project.assets.build
      respond_to do |format|
          if @project.save
              format.html { redirect_to @project, notice: 'Project was successfully created.' }
              format.json { render :show, status: :created, location: @project }
          else 
              format.html { render :new }
              format.json { render json: @project.errors, status: :unprocessable_entity }
          end 
end


def project_params
  params.require(:project).permit(:user_id,  :summary, :start_date,assets_attributes: [:id, :project_id, :attachment,:user_id] )
end

这就是表单的样子

   <%= form_for @project,:html => {:multipart => true } do |f| %> 
     <% if @project.errors.any? %>
     <div id="error_explanation">
     </div>
     <% end %>
     <%= f.fields_for :assets do |p| %>
         <div class="field">
             <%= p.label :attachment %><br>
             <%= p.file_field :attachment,name: "assets[attachment][]" %>
          </div>
     <% end %>
     <div class="actions">
         <%= f.submit %>
      </div>
    <% end %>

1 个答案:

答案 0 :(得分:0)

<强> schema.rb

create_table "assets", force: true do |t|
    t.string   "assetable_id"
    t.string   "assetable_type"
    t.string   "attachment"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

create_table "projects", force: true do |t|
    t.string   "user_id"
    t.string   "summary"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

<强>项目/ _form.html.erb

 <%= form_for @project, :html => {:multipart => true } do |f| %>
 <% if @project.errors.any? %>
    <div id="error_explanation">
      <%= @project.errors.inspect %>
    </div>
 <% end %>

 <%= f.label :summary %>
 <%= f.text_field :summary %>

 <%= f.fields_for :assets do |p| %>
   <div class="field">
     <%= p.label :attachment %><br>
     <%= p.file_field :attachment %>

     <%= p.hidden_field :assetable_id %>
     <%= p.hidden_field :assetable_type %>
    </div>
 <% end %>

 <div class="actions">
     <%= f.submit %>
  </div>
<% end %>

<强> projects_controller.rb

  # GET /projects/new
  def new
    @project = Project.new
    @project.assets.build
  end

  # POST /projects
  # POST /projects.json
  def create
    @project = Project.new(project_params)
    respond_to do |format|
      if @project.save
          format.html { redirect_to @project, notice: 'Project was successfully created.' }
          format.json { render :show, status: :created, location: @project }
      else
          format.html { render :new }
          format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

  private

    # Never trust parameters from the scary internet, only allow the white list through.
    def project_params
      params.require(:project).permit(:user_id,  :summary, :start_date, assets_attributes: [:id, :assetable_id, :assetable_type, :attachment, :user_id] )
    end

<强> project.rb

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

<强> asset.rb

class Asset < ActiveRecord::Base
  belongs_to :project
  belongs_to :assetable, :polymorphic => true

  mount_uploader :attachment, AttachmentUploader #carrierwave

  validates :attachment, presence: true
  validates :assetable_type, presence: true
end

<强>除了

由于您根据上一个问题提出了这个问题,您只是提到:如果您希望资产类的实例属于不同类型的类(即其他类似的东西),您只想使用多态关联而不是一个项目)。