Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails:多态关联并接受嵌套的属性_Ruby On Rails_Ruby On Rails 4_Rails Activerecord_Nested Attributes_Polymorphic Associations - Fatal编程技术网

Ruby on rails Rails:多态关联并接受嵌套的属性

Ruby on rails Rails:多态关联并接受嵌套的属性,ruby-on-rails,ruby-on-rails-4,rails-activerecord,nested-attributes,polymorphic-associations,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

附件无法保存。我错过了什么? 在我的应用程序中,我有一个项目,每个项目用户都可以上传许多资产。上传是通过载波完成的

这些是模型

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
这就是表单的外观

   <%= 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

项目/_form.html.erb

project.rb

asset.rb

旁白


由于您是根据之前的问题提出这个问题的,所以我只想提一下:如果您希望资产类别的实例属于不同类型的类别,即项目以外的类别,那么您只想使用多态关联

您的表单是否包含以下内容:enctype=多部分/表单数据?否则二进制数据将不会与post VAR一起发送。谢谢您的评论,我已经更新了问题。所以表格也在那里,是的,我有其他的模型,我想用资产来做。这就是为什么我需要使用多态关联我有一个问题,当我想上传一个文件时,我得到了这个错误,资产附件不能为空。你的资产类中有一个验证,以确保它有一个当前的附件。验证:附件,存在:真你得到了这个错误,因为你的表单提交不包括图像。您是否单击文件上载按钮并选择图像?如果是这样,请检查development.log文件中是否有CarrierWave的错误,如果有问题,请提出新问题。如果你觉得他们回答了你的问题,不要忘记接受答案
   <%= 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 %>
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_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 %>
  # 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
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 :assetable, :polymorphic => true

  mount_uploader :attachment, AttachmentUploader #carrierwave

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