在Rails 3中使用回形针使用accept_nested_attributes保存多个图像 [英] saving multiple images using accept_nested_attributes using paperclip in rails 3

查看:69
本文介绍了在Rails 3中使用回形针使用accept_nested_attributes保存多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型

########## 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后回滚...仍然想知道出什么问题了.

i am working on saving multiple image uploads using @image.image_assets.build instead of #3.times {@image.image_assets.build} as i dont wont my users to see three upload button,Hence i am using multiple=>true so that i can get an array of uploads and then save it using the association image.image_assets.build but unable to save the parent ie @image..i even tried inverse_of ,but i dont think i need it...when i place the loop to save the nested association,i can see the insert queries but upon saving the parent ie @image..it rollbacks...still wondering whats wrong..

 ########## 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上使用回形针3.5添加多个图像上传.但是它不起作用,并给了我 SystemStackError(堆栈级别太深)错误.我知道我的关联有问题,但我尽力弄清楚,但对我来说似乎是对的...所以我决定在此处发布此信息,以获取帮助,以了解其为何不起作用.或通过其他任何方式实现单个模型的多次上传

i know,its very simple but i wont to add multiple image uploads using paperclip 3.5 on rails 3.2.BUT its not working and giving me SystemStackError (stack level too deep) error.i know there is something wrong with my associations but i tried hard to figure out but it seems right to me...so i decided to post this here to get some help as why its not working.or any other way to implement multiple uploads with single model

推荐答案

...我发现是曲别针版本3.5.3 造成了问题...所以当我将其更改为 paperclip> = 4 ...没有错误,现在我可以立即保存父项及其关联....好吧,我不知道这是什么问题..但如果要对更高版本的回形针

after struggling ...i figured out that it was the paperclip version 3.5.3 that was creating problem...so when i changed it to paperclip >= 4...there was no error and now i am able to save the parent and its association in a go....well i dont know what was the issue..but if you want to do the same ..go for higher version of paperclip

这篇关于在Rails 3中使用回形针使用accept_nested_attributes保存多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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