I'm new to rails. I want to know about file uploading process in rails. Can anyone please help me... Thanks, Althaf
How to upload a file in rails?
Asked Answered
Check out Ruby on Rails Guides (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. –
Lowgrade
Or 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. –
Chilblain
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| %>
=>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 –
Ostyak
@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. –
Utoaztecan
@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 :) –
Worthless
Just to add to this old answer, there's a newer gem that is better at handling file uploading: Carrierwave. –
Prolusion
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. –
Mania
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.
© 2022 - 2024 — McMap. All rights reserved.