Rails 3 - nested model - has_many - download jquery file

I have a Post model that has_many: photos. When creating a new post, the user must also be able to select photos for this post.

I am using RAILS 3.2.9, nested_form, carrierwave and jquery-fileupload-rails gem and ryan bates railscasts as a guide.

It seems that everything is configured correctly, but the problem is that when the user selects a photo (the fileupload () function fires), a new record and a new record are created. When I click Create Message, another entry is created again.

Any help / idea appreciated.

Many thanks.

Peter

class Post < ActiveRecord::Base
  has_many :photos, as: :attachable, :dependent => :destroy
  accepts_nested_attributes_for :photos, :allow_destroy => true
end

class Photo < ActiveRecord::Base
  belongs_to :attachable, polymorphic: true
  attr_accessible :image, :description, :post_id, :attachable_id, :attachable_type
  mount_uploader :image, PhotoUploader
end


# Post Controller
def create
  @post = Post.new(params[:post])
  @post.save
end


# _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();
+5
source share
1

https://github.com/blueimp/jQuery-File-Upload/blob/master/js/jquery.fileupload.js#L140

Ajax ( ), (. ). , .

? AJAX, . link_to_add link_to_remove .

EDIT:

, ( , ), .

, , , , . , -, javascript , Post , , (, , !).

, , ( , 5-10 , - post)... , , AJAX (, ).

, , , . jQuery, , , .

+5

All Articles