Created
May 23, 2014 07:02
-
-
Save 2bethere/b64e3907e7c436d9a0b9 to your computer and use it in GitHub Desktop.
Multiple file upload for Rails 4 params
This file contains 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 Image < ActiveRecord::Base | |
belongs_to :post | |
has_attached_file :file, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png" | |
validates_attachment_content_type :file, :content_type => /\Aimage\/.*\Z/ | |
end |
This file contains 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, :url => posts_path,:html => { :multipart => true } do |f| %> | |
<% if @post.errors.any? %> | |
<div class="sectionwrapper errorbox"> | |
<div id="errorExplanation"> | |
<div class="title"><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved: | |
</div> | |
<ul> | |
<% @post.errors.full_messages.each do |msg| %> | |
<li><%= msg %></li> | |
<% end %> | |
</ul> | |
</div> | |
</div> | |
<% end %> | |
<div class="sectionwrapper"> | |
<%= f.label "Add pictures" ,:class =>"inputlabel" %> | |
<%= f.fields_for :images do |i| %> | |
<% if i.object.new_record? %> | |
<%= i.file_field :file , :multiple => true%> | |
<% else %> | |
<div class="imagescontainer sectionwrapper"> | |
<% @post.images.each do |image| %> | |
<%= image_tag image.file.url %> | |
<% end %> | |
</div> | |
<% end %> | |
<% end %> | |
</div> | |
<div class="mid submitwrapper"> | |
<%= f.submit "" %> | |
</div> | |
<% end %> |
This file contains 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 | |
#after_update :save_images | |
default_scope {order('created_at DESC')} | |
belongs_to :user | |
has_many :images | |
accepts_nested_attributes_for :images, allow_destroy: true | |
validates :post_content, presence: true | |
def save_images | |
images.each do |image| | |
image.save() | |
end | |
end | |
end |
This file contains 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 | |
layout "posts" | |
=begin | |
Default RUD code being generated. Not shown here as I'm too lazy to rewrite production code and remove sections. | |
=end | |
# POST /posts | |
# POST /posts.json | |
def create | |
# Save all images | |
@post = Post.new | |
@post.user = current_user | |
respond_to do |format| | |
if @post.save | |
if(post_params[:images_attributes]) | |
post_params[:images_attributes]["0"]["file"].each do |i| | |
image = Image.new | |
image.file = i | |
image.post = @post | |
image.save() | |
end | |
end | |
format.html { redirect_to root_path, notice: 'Post was successfully created.' } | |
format.json { render action: 'show', status: :created, location: @post } | |
else | |
format.html { render action: 'new' } | |
format.json { render json: @post.errors, status: :unprocessable_entity } | |
end | |
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(:post_content, :test_result_cholera_o1, :test_result_cholera_o139, | |
:test_result_nitrite, :test_result_coliform, :test_result_arsenic, | |
:location, | |
# This line turns out to be key to make rails 4 params work for mutliple files. | |
images_attributes: [:file => []] ) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've created this as I've spend almost 2 hours trying to figure out how to do multiple file upload with Rail 4 using params.
The problem here is on line 47 of posts_controller.rb. You need to properly specify this to match a file field that allows selection of multiple files for upload. All the rest is easy, just process all the pictures and save.
Note this is a naive approach for image processing as you should not block response while waiting for image processing to complete. You should setup resque to do the processing in background while you return success to the user.