Last active
August 29, 2015 14:04
-
-
Save rishighan/174f239dc62a24e6c9f5 to your computer and use it in GitHub Desktop.
dropzone + paperclip multiple image upload issue.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <%= form_for(@post, html: {class:"dropzone"}) do |f| %> | |
| <% if @post.errors.any? %> | |
| <div class="error_explanation"> | |
| <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2> | |
| <ul> | |
| <% @post.errors.full_messages.each do |msg| %> | |
| <li><%= msg %></li> | |
| <% end %> | |
| </ul> | |
| </div> | |
| <% end %> | |
| <p> | |
| <%= f.label :title %> <br/> | |
| <%= f.text_field(:title, :size => "70") %> | |
| </p> | |
| <p class="well"> | |
| <%= f.label :category %><br/> | |
| <%= hidden_field_tag "post[category_ids][]", nil%> | |
| <% Category.order(:title).each do |cat| %> | |
| <label> | |
| <%= check_box_tag("post[category_ids][]", cat.id, cat.id.in?(@post.categories.collect(&:id))) %> | |
| <%= cat.title %> | |
| </label> | |
| <% end %> | |
| </p> | |
| <p> | |
| <%= f.label :content %> <br/> | |
| <%= f.text_area(:content, :size => "90x12") %> | |
| <div class="fallback"> | |
| <%= f.label :attachments %> | |
| <%= f.fields_for :attachments do |at| %> | |
| <%= at.file_field :picture %> | |
| <% end %> | |
| </div> | |
| <%= render "shared/markdownhelp" %> | |
| </p> | |
| <p> | |
| <%= f.label :excerpt %><br/> | |
| <%= f.text_area(:excerpt, :size => "90x8") %> | |
| </p> | |
| <p> | |
| <%= f.submit %> | |
| </p> | |
| <% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Attachment < ActiveRecord::Base | |
| include Rails.application.routes.url_helpers | |
| belongs_to :imageable | |
| has_attached_file :picture, | |
| :styles =>{ :medium => "660x", | |
| :thumb => "150x" | |
| }, | |
| :url => "/attachments/pictures/:style/:basename.:extension", | |
| :path =>"#{Rails.root}/public/attachments/pictures/:style/:basename.:extension" | |
| validates_attachment_content_type :picture, :content_type => /\Aimage\/.*\Z/ | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| post"=>{"title"=>"", "category_ids"=>[""], "content"=>"", "excerpt"=>"", "attachments_attributes"=>[{"picture"=>#<ActionDispatch::Http::UploadedFile:0x00000101a5c240 @tempfile=#<Tempfile:/tmp/RackMultipart20140729-45451-f62ok4>, @original_filename="IMG_0897.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"post[attachments_attributes][][picture]\"; filename=\"IMG_0897.JPG\"\r\nContent-Type: image/jpeg\r\n">}]}, "commit"=>"Create Post"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Post < ActiveRecord::Base | |
| has_many :attachments, as: :imageable, dependent: :destroy | |
| accepts_nested_attributes_for :attachments | |
| has_and_belongs_to_many :categories, join_table: :categories_posts | |
| accepts_nested_attributes_for :categories | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class PostsController < ApplicationController | |
| before_action :set_post, only: [:show, :edit, :update, :destroy] | |
| layout "admin" | |
| # GET /posts/new | |
| def new | |
| @post = Post.new | |
| @post.attachments.build | |
| end | |
| # POST /posts | |
| # POST /posts.json | |
| def create | |
| @post = Post.new(post_params) | |
| if @post.save | |
| render json: { message: "success"}, :status => 200 | |
| else | |
| # you need to send an error header, otherwise Dropzone | |
| # will not interpret the response as an error: | |
| render json: { error: @post.errors.full_messages.join(',')}, :status => 400 | |
| end | |
| end | |
| private | |
| # Use callbacks to share common setup or constraints between actions. | |
| def set_post | |
| @post = Post.find(params[:id]) | |
| end | |
| # Never trust parameters from the scary internet, only allow the white list through. | |
| def post_params | |
| params.require(:post).permit(:title, :content, :excerpt, {:category_ids=>[]}, attachments_attributes:[:picture]) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $(document).ready(function(){ | |
| // disable auto discover | |
| Dropzone.autoDiscover = false; | |
| // grap our upload form by its id | |
| $("#new_post").dropzone({ | |
| // restrict image size to a maximum 1MB | |
| maxFilesize: 1, | |
| // changed the passed param to one accepted by | |
| // our rails app | |
| paramName: "post[attachments_attributes][][picture]", | |
| // show remove links on each image upload | |
| addRemoveLinks: true | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment