Rails:多态关联和accepts_nested_attributes [英] Rails: Polymorphic assosiation and accepts_nested_attributes

本文介绍了Rails:多态关联和accepts_nested_attributes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The attachment won't save. What I am missing here? In my application I have a project ,for each project user can upload many assets. The Upload is done by carrier wave.

这是模型

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中的操作

and these are the actions in my 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 %>

推荐答案

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

projects/_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

在旁边

由于您是根据之前提出的问题来回答这个问题的,因此我将仅提及:如果您打算让资产类别的实例属于不同类型的类别(例如,项目).

Since you based this question off a previous question you asked I'll just mention: You only want to use a polymorphic association if you intend instances of your asset class to belong to different types of classes (ie. things other than a project).

这篇关于Rails:多态关联和accepts_nested_attributes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆