How to upload a file to the rails?

I am new to rails. I want to learn about the file upload process in rails. Can anyone help me please ... Thanks, Althaf

+2
source share
2 answers

Typically, gems / plugins are used to handle file uploads. My favorite and perhaps the most ubiquitous Paperclip .

In your opinion, you will need to tell the rails that you are loading the file as follows:

<%= form_for @model, :html => { :multipart => true } do |form| %> 
+5
source

Here is a way to download a file without using any gem and just 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 the views should contain this, it's very simple,

  <%= form_for @photo do |f| %> <%= f.file_field :photo %> <div class="actions"> <%= f.submit "Upload" %> </div> <% end %> 

and finally, the model must have

  has_attached_file :image 

###################################################. #### Now you can upload any file.

Thanks. Play with the 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. 
+1
source

Source: https://habr.com/ru/post/1498642/


All Articles