0

So I'll try to be as specific as possible.

I'm building a website and I'd like to implement a upload for users to upload images as an avatar or a screenshot in their comment. I'm still quite a beginner (coding for 7 weeks) and I'm writing this website using BackboneJS and Ruby on Rails. I think I need just a form and an input tag within the HTML template that allows users to select a picture from their local system to my application. My question is, once I parse the data (assuming I parse it because Ive been parsing all data into JSON).

Where does it go?

I have read that its bad to store the image into my postgreSQL database.

But where else would it go in the back-end?

Is my application just going to download what they upload and keep it in a folder that is assigned to each specific user?

If anybody could explain it at a pretty basic level, I would appreciate it. Thanks!

1 Answer 1

1

When I was learning how to do file upload on rails this was really helpful for me carrierwave-file-uploads

You will need to add this 2 gems to your gem file

gem "rmagick"
gem "carrierwave"

Create a uploader
app/uploaders/image_uploader.rb

class ImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::RMagick

  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  version :thumb do
    process :resize_to_limit => [200, 200]
  end
end

last but not least you need your form

<%= form_for @painting, :html => {:multipart => true} do |f| %>
  <%= f.error_messages %>
  <%= f.hidden_field :gallery_id %>
  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
  <p>
    <%= f.file_field :image %>
  </p>
  <p>
    <%= f.label :remote_image_url, "or image URL" %><br />
    <%= f.text_field :remote_image_url %>
  </p>
  <p><%= f.submit %></p>
<% end %>

I hope that this helps. Keep on coding. You are doing great for 7 weeks

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.