Skip to content

Instantly share code, notes, and snippets.

@davidahouse
Created April 28, 2013 15:25
Show Gist options
  • Save davidahouse/5477208 to your computer and use it in GitHub Desktop.
Save davidahouse/5477208 to your computer and use it in GitHub Desktop.
Upload an image using Rails & Parse
# Add image field to model
# fields :title, :some_image_field
# Create a form for uploading the image
<%= form_tag({ :action => :upload_image_save}, :multipart => true) do %>
<%= file_field_tag 'image' %>
<div class="actions">
<%= submit_tag %>
</div>
<% end %>
# Create a controller method for the save
@obj = Model.find(params[:id])
image_file = params[:image]
result = Model.upload(image_file.tempfile,image_file.original_filename,content_type: image_file.content_type)
@obj.some_image_field = {"name" => result["name"], "__type" => "File"}
respond_to do |format|
if @obj.save
format.html { redirect_to models_path, notice: 'Image uploaded' }
else
format.html { redirect_to models_path, notice: 'Error uploading image' }
end
end
# Show the image somewhere the field is a URL to the image
<img src=<%= obj.some_image_field %>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment