Forum: Ruby on Rails rails 3 - nested model - jquery file upload

Ed78f6c66a6f29bd89c67eeaf4f69931?d=identicon&s=25 Petr Bobek (praethorian)
on 2013-01-29 18:00
(Received via mailing list)
I have a Post model which has_many :photos. While User creating a new
post,
user should be also able to select photos (multiple) for given post.

I am using RAILS 3.2.9, nested_form, carrierwave and
jquery-fileupload-rails gem and ryan bates
railscasts<http://railscasts.com/episodes/381-jquery-file-upload> as
a guide.

All seems to be set up correctly, but problem is, when User choose a
photo
(a fileupload() function is triggered), new Post and new Photo record
are
created. Once I press "create post" another post record is again
created. Any
help/idea how to get rid off with the first Post record once user select
the photo?

Thank you very much.

Petr


class Post < ActiveRecord::Base
  has_many :photos, as: :attachable, :dependent => :destroy
  accepts_nested_attributes_for :photos, :allow_destroy => trueend
class Photo < ActiveRecord::Base
  belongs_to :attachable, polymorphic: true
  attr_accessible :image, :description, :post_id, :attachable_id,
:attachable_type
  mount_uploader :image, PhotoUploaderend

# Post Controllerdef create
  @post = Post.new(params[:post])
  @post.saveend

# _form.html.erb<%= nested_form_for @post, :html => { :multipart => true
} do |f| %>
  <%= f.fields_for :photos do |photo| %>
    <% if photo.object.new_record? %>
      <%= photo.file_field :image, id: "fileupload" %>
      <%= photo.hidden_field :id %>
      <%= photo.hidden_field :attachable_id %>
      <%= photo.hidden_field :attachable_type %>
    <% else %>
      <%= image_tag(photo.object.image.url(:thumb)) %>
      <%= photo.check_box :_destroy %>
    <% end %>
  <% end %><% end %>
#application.js
$('#fileupload').fileupload();
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.