{"id":7779,"date":"2015-10-25T20:05:09","date_gmt":"2015-10-25T20:05:09","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/10\/25\/rails-4-multiple-image-or-file-upload-using-carrierwave-open-source-projects-carrierwaveuploader-carrierwave\/"},"modified":"2015-10-25T20:05:09","modified_gmt":"2015-10-25T20:05:09","slug":"rails-4-multiple-image-or-file-upload-using-carrierwave-open-source-projects-carrierwaveuploader-carrierwave","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/10\/25\/rails-4-multiple-image-or-file-upload-using-carrierwave-open-source-projects-carrierwaveuploader-carrierwave\/","title":{"rendered":"Rails 4 multiple image or file upload using carrierwave-open source projects carrierwaveuploader\/carrierwave"},"content":{"rendered":"<blockquote>\n<p><strong>This is solution to upload multiple images using carrierwave in rails 4 from scratch<\/strong><\/p>\n<p><strong>Or you can find working demo : Multiple Attachment Rails 4<\/strong><\/p>\n<\/blockquote>\n<p><strong>To do just follow these steps.<\/strong><\/p>\n<pre><code>rails new multiple_image_upload_carrierwave\n<\/code><\/pre>\n<p><strong>In gem file<\/strong><\/p>\n<pre><code>gem 'carrierwave'\nbundle install\nrails generate uploader Avatar \n<\/code><\/pre>\n<p><strong>Create post scaffold<\/strong><\/p>\n<pre><code>rails generate scaffold post title:string\n<\/code><\/pre>\n<p><strong>Create post_attachment scaffold<\/strong><\/p>\n<pre><code>rails generate scaffold post_attachment post_id:integer avatar:string\n\nrake db:migrate\n<\/code><\/pre>\n<p><strong>In post.rb<\/strong><\/p>\n<pre><code>class Post &lt; ActiveRecord::Base\n   has_many :post_attachments\n   accepts_nested_attributes_for :post_attachments\nend\n<\/code><\/pre>\n<p><strong>In post_attachment.rb<\/strong><\/p>\n<pre><code>class PostAttachment &lt; ActiveRecord::Base\n   mount_uploader :avatar, AvatarUploader\n   belongs_to :post\nend\n<\/code><\/pre>\n<p><strong>In post_controller.rb<\/strong><\/p>\n<pre><code>def show\n   @post_attachments = @post.post_attachments.all\nend\n\ndef new\n   @post = Post.new\n   @post_attachment = @post.post_attachments.build\nend\n\ndef create\n   @post = Post.new(post_params)\n\n   respond_to do |format|\n     if @post.save\n       params[:post_attachments]['avatar'].each do |a|\n          @post_attachment = @post.post_attachments.create!(:avatar =&gt; a, :post_id =&gt; @post.id)\n       end\n       format.html { redirect_to @post, notice: 'Post was successfully created.' }\n     else\n       format.html { render action: 'new' }\n     end\n   end\n end\n\n private\n   def post_params\n      params.require(:post).permit(:title, post_attachments_attributes: [:id, :post_id, :avatar])\n   end\n<\/code><\/pre>\n<p><strong>In views\/posts\/_form.html.erb<\/strong><\/p>\n<pre><code> { :multipart =&gt; true }) do |f| %&gt;\n   \n     <br \/>\n     \n   \n\n   \n     \n       <br \/>\n        true, name: \"post_attachments[avatar][]\" %&gt;\n     \n   \n\n   \n     \n   \n\n<\/code><\/pre>\n<p>To edit an attachment and list of attachment for any post. <strong>In views\/posts\/show.html.erb<\/strong><\/p>\n<pre><code><br id=\"notice\" \/><br \/><br \/>\n\n<br \/>\n  <strong>Title:<\/strong>\n  \n<br \/><br \/>\n\n\n  \n  \n\n\n |\n\n<\/code><\/pre>\n<p>Update form to edit an attachment <strong>views\/post_attachments\/_form.html.erb<\/strong><\/p>\n<pre><code>\n\n  \n    <br \/>\n    \n  \n  \n    \n  \n\n<\/code><\/pre>\n<p>Modify update method in <strong>post_attachment_controller.rb<\/strong><\/p>\n<pre><code>def update\n  respond_to do |format|\n    if @post_attachment.update(post_attachment_params)\n      format.html { redirect_to @post_attachment.post, notice: 'Post attachment was successfully updated.' }\n    end \n  end\nend\n<\/code><\/pre>\n<p>In rails 3 no need to define strong parameters and as you can define attribute_accessible in both the model and accept_nested_attribute to post model because attribute accessible is deprecated in rails 4.<\/p>\n<p>For edit an attachment we cant modify all the attachments at a time. so we will replace attachment one by one, or you can modify as per your rule, Here I just show you how to update any attachment.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is solution to upload multiple images using carrierwave in rails 4 from scratch Or you can find working demo : Multiple Attachment Rails 4 To do just follow these steps. rails new multiple_image_upload_carrierwave In gem file gem &#8216;carrierwave&#8217; bundle install rails generate uploader Avatar Create post scaffold rails generate scaffold post title:string Create post_attachment [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7779","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7779","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7779"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7779\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7779"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}