i have two models
########## 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
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)
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