Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 - nested model - has_many - jquery file upload
    primarykey
    data
    text
    <p>I have a Post model which has_many :photos. While User creating a new post, user should be also able to select photos for given post.</p> <p>I am using RAILS 3.2.9, nested_form, carrierwave and jquery-fileupload-rails gem and ryan bates <a href="http://railscasts.com/episodes/381-jquery-file-upload">railscasts</a> as a guide.</p> <p>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.</p> <p>Any help/idea is appreciated.</p> <p>Thank you very much.</p> <p>Petr</p> <pre><code>class Post &lt; ActiveRecord::Base has_many :photos, as: :attachable, :dependent =&gt; :destroy accepts_nested_attributes_for :photos, :allow_destroy =&gt; true end class Photo &lt; 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 &lt;%= nested_form_for @post, :html =&gt; { :multipart =&gt; true } do |f| %&gt; &lt;%= f.fields_for :photos do |photo| %&gt; &lt;% if photo.object.new_record? %&gt; &lt;%= photo.file_field :image, id: "fileupload" %&gt; &lt;%= photo.hidden_field :id %&gt; &lt;%= photo.hidden_field :attachable_id %&gt; &lt;%= photo.hidden_field :attachable_type %&gt; &lt;% else %&gt; &lt;%= image_tag(photo.object.image.url(:thumb)) %&gt; &lt;%= photo.check_box :_destroy %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;% end %&gt; #application.js $('#fileupload').fileupload(); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload