在rails 3中使用paperclip使用accept_nested_attributes保存多个图像

时间:2014-08-17 09:28:13

标签: ruby-on-rails ruby-on-rails-3 paperclip image-uploading

我有两个型号

########## image.rb

has_many :image_assets , :dependent => :destroy 
accepts_nested_attributes_for :image_assets, :reject_if => lambda { |a| a[:avatar].blank? }, :allow_destroy => true

########## image_assets.rb  

belongs_to :image  
has_attached_file :avatar,
 :styles => {:view => "187x260#"},
:storage => :s3,
:s3_permissions => :private,
:s3_credentials => S3_CREDENTIALS

 attr_accessible :image_id,:avatar,:avatar_file_name,:avatar_content_type,:avatar_file_size,:avatar_updated_at


##########   in my controller
def new
    @image = Image.new
    #3.times {@image.image_assets.build} # added this
    @image.image_assets.build
    respond_to :js
end

def create
   @image = Image.new(params[:image])  
   @image.user_id = current_or_guest_user.id
  ##########this works but no use as parent is not getting save..ie @image
  # if params[:image_assets][:avatar]
  #     params[:image_assets][:avatar].each do |avatar|
  #       @image.image_assets.build(:avatar=> avatar)
  #     end 
  # end
  ########here i am unable to save the @image 
   if @image.save
       if params[:image_assets][:avatar]
        params[:image_assets][:avatar].each { |image|
          @image.image_assets.create(avatar: image)
        }
      end
     redirect_to @image
  else
     flash[:alert] = "Something went wrong."
      render :new
  end 

end

我正在使用@image.image_assets.build代替#3.times {@image.image_assets.build}保存多个图片上传,因为我不会让我的用户看到三个上传按钮,因此我使用multiple=>true以便我可以获得一组上传,然后使用关联image.image_assets.build保存它,但无法保存父,即@ image..i甚至尝试了inverse_of,但我不认为我需要它...当我放置循环来保存嵌套关联,我可以看到插入查询,但保存父亲即@ image..it回滚...仍然想知道什么是错的..

 ########## my _form.html.erb
<%= form_for(@image,:html=>{:multipart => true,:remote=>true,:class=>"form-horizontal",:role=>"form"}) do |f |%> 
  <div class="form-group">   
     <%= f.fields_for :image_assets do |builder| %>   
      <% if builder.object.new_record? %>   
        Upload picture
        <%= builder.file_field :avatar,:multiple=>"true",:name=>"image_assets[avatar] []",:class=>"opacity"%>
    <% end %>  
<% end %>
<% end %>     

 ##########this is the error stack i have


Started POST "/images" for 127.0.0.1 at 2014-08-17 02:03:17 -0700
Processing by ImagesController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"MbZz1iMRVhGgl+WniARp8+FZaaDFtyYtodxxzGzo57w=", "image"=>{"title"=>"hiii", "about"=>"ddddddd", "tag_list"=>"2"}, "remotipart_submitted"=>"true", "X-Requested-With"=>"IFrame", "X-Http-Accept"=>"text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01", "image_assets"=>{"avatar"=>[#<ActionDispatch::Http::UploadedFile:0x0000000656ee28 @original_filename="big_image.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"image_assets[avatar][]\"; filename=\"big_image.jpeg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/tmp/RackMultipart20140817-25517-1xcwosa>>, #<ActionDispatch::Http::UploadedFile:0x0000000656edb0 @original_filename="cool.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"image_assets[avatar][]\"; filename=\"cool.jpeg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/tmp/RackMultipart20140817-25517-t9skis>>, #<ActionDispatch::Http::UploadedFile:0x0000000656ed38 @original_filename="cool.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"image_assets[avatar][]\"; filename=\"cool.png\"\r\nContent-Type: image/png\r\n", @tempfile=#<File:/tmp/RackMultipart20140817-25517-16flp1w>>]}}
[Shard: slave_development_2]  User Load (2.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 7 LIMIT 1
[Shard: slave_development_1]  ActsAsTaggableOn::Tag Load (2.6ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" IS NULL AND "taggings"."taggable_type" = 'Image' AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)
   (1.0ms)  BEGIN
  PicCategory Load (49.2ms)  SELECT "pic_categories".* FROM "pic_categories" WHERE "pic_categories"."id" = $1 LIMIT 1  [["id", "2"]]
  SQL (48.3ms)  INSERT INTO "images" ("about", "avatar_processing", "created_at", "notified", "title", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["about", "ddddddd"], ["avatar_processing", nil], ["created_at", Sun, 17 Aug 2014 09:03:17 UTC +00:00], ["notified", "NO"], ["title", "hiii"], ["updated_at", Sun, 17 Aug 2014 09:03:17 UTC +00:00], ["user_id", 7]]
  SOLR Request (782.4ms)  [ path=update parameters={} ]
  ActsAsTaggableOn::Tag Load (10.5ms)  SELECT "tags".* FROM "tags" WHERE (lower(name) = 'abstract')
  ActsAsTaggableOn::Tag Load (1.6ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 12 AND "taggings"."taggable_type" = 'Image' AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)
  ActsAsTaggableOn::Tagging Exists (2.7ms)  SELECT 1 AS one FROM "taggings" WHERE ("taggings"."tag_id" = 3 AND "taggings"."taggable_type" = 'Image' AND "taggings"."taggable_id" = 12 AND "taggings"."context" = 'tags' AND "taggings"."tagger_id" IS NULL AND "taggings"."tagger_type" IS NULL) LIMIT 1
  SQL (20.4ms)  INSERT INTO "taggings" ("context", "created_at", "tag_id", "taggable_id", "taggable_type", "tagger_id", "tagger_type") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["context", "tags"], ["created_at", Sun, 17 Aug 2014 09:03:19 UTC +00:00], ["tag_id", 3], ["taggable_id", 12], ["taggable_type", "Image"], ["tagger_id", nil], ["tagger_type", nil]]
   (1.2ms)  ROLLBACK
Completed 500 Internal Server Error in 1841ms

SystemStackError (stack level too deep):
  actionpack (3.2.13) lib/action_dispatch/middleware/reloader.rb:70


  Rendered /home/ubuntu/.rvm/gems/ruby-1.9.3-p545@latest/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.1ms)
  Rendered /home/ubuntu/.rvm/gems/ruby-1.9.3-p545@latest/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.3ms)
  Rendered /home/ubuntu/.rvm/gems/ruby-1.9.3-p545@latest/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (11.1ms)

我知道,它非常简单,但我不会在rails 3.2上使用paperclip 3.5添加多个图片上传。但它不工作并且给我SystemStackError (stack level too deep) error.i知道我的协会有问题但我试过很难弄清楚,但它似乎对我来说...所以我决定在这里发布这个以获得一些帮助,因为它不工作。或任何其他方式实现单个模型的多个上传

1 个答案:

答案 0 :(得分:0)

挣扎之后......我发现创造问题的是paperclip version 3.5.3 ......所以当我把它改成paperclip >= 4时......没有错误,现在我能够保存父母及其关联...好吧我不知道是什么问题..但如果你想做同样..go for 更高版本的回形针