I'm new to rails. I want to know about file uploading process in rails. Can anyone please help me... Thanks, Althaf
Asked
Active
Viewed 2.1k times
3
-
6Check out Ruby on Rails Guides (http://guides.rubyonrails.org/form_helpers.html#uploading-files) or google "rails file uploading" or "rails file upload plugins" to learn more. It would be best to do this research first, then come back here to ask more specific questions about the parts you need to know more about. – Jeff Oct 14 '10 at 12:38
-
2Or just search Stackoverflow for other questions on that topic ("rails file upload"). I believe you must've seen those questions popping up when you were typing yours. – Matt Oct 14 '10 at 13:00
2 Answers
6
Usually gems/plugins are used to to handle file uploads. My favorite one, and perhaps the most ubiquitous is Paperclip.
In your view, you'll have to tell the rails form helpers that you're uploading a file like this:
<%= form_for @model, :html => { :multipart => true } do |form| %>

Jason stewart
- 1,085
- 8
- 8
-
=>Thanks for your reply. I wanted to know the uploading code that is to be written in the controller and in model(if needed). I'm familiar with view part for file uploading – althaf_tvm Oct 14 '10 at 12:43
-
@user475748 dude u dont need to write any special lines of code for uploading the file. Paperclip plugin has those lines of code. Just follow the blogpost i provided in my answer and check that 5 mins railscast. – Mohit Jain Oct 14 '10 at 12:46
-
@user475748: follow the instructions for the Paperclip gem. It handles the internal details of file uploads, letting you simply specify configuration values and letting it do the rest :) – Matchu Oct 15 '10 at 02:08
-
4Just to add to this old answer, there's a newer gem that is better at handling file uploading: [Carrierwave](https://github.com/jnicklas/carrierwave). – XåpplI'-I0llwlg'I - Aug 03 '12 at 03:30
-
Hi Jason, you don't have to specify { :multipart => true } with a form_for helper, rails will add that automatically if you have file_field. It is only necessary if you use form_tag. – Hunter Jun 27 '14 at 04:01
1
Here is a method on how to upload file without using any gem and only by using rails,
Solution :=>
def create
@photo = Photo.new(photo_params)
uploaded_io = params[:photo][:photo]
File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'wb') do |file|
file.write(uploaded_io.read)
end
if @photo.save
flash[:success] = "The photo was added!"
redirect_to root_path
else
render 'new'
end
end
def upload
uploaded_io = params[:person][:picture]
File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'wb') do |file|
file.write(uploaded_io.read)
end
end
And your form.html.erb in views should contain this, it is very simple,
<%= form_for @photo do |f| %>
<%= f.file_field :photo %>
<div class="actions">
<%= f.submit "Upload" %>
</div>
<% end %>
and finally the model should have ,
has_attached_file :image
.################################################## You can now enjoy loading any file .
Thank you. Have funn with rails.
Use <video_tag> for viewing video files.
Use <audio_tag> for viewing audio files.
Use <object>"link"</object> for viewing PDF or DOC files.

Ballaji T
- 49
- 9