Rails: how to load a previously loaded document?

I need my user to upload such documents in pdf and txt format to their profile. And I did this with Carrierwave, so I have a list of documents with titles and URLs.

But how can I get other users to upload these files? Should I use a gem or is there something native that I don’t even know about, as I am very new to rails?

thanks

EDIT:

society.rb

class Society < ActiveRecord::Base ... has_many :documents, :dependent => :destroy end 

document.rb

 class Document < ActiveRecord::Base belongs_to :society mount_uploader :url, DocumentUploader end 

And then this in the view I want to download files:

  <% @society.documents.each do |doc| %> <%= link_to "Download it", doc.url %> //url is the column name of the saved url <% end %> 
+7
source share
3 answers

I do not use Carrierwave, but I think it looks like Paperclip.

So you should be able to use something like this

  link_to 'Download file', user.file.url 

It is assumed that you have a user instance object from the model with the 'file' carrierwave attribute. Replace this with the name of your attribute.

+11
source

You can use the send_file method to send_file . It might look like this:

 class MyController def download_file @model = MyModel.find(params[:id]) send_file(@model.file.path, :filename => @model.file.name, :type => @model.file.content_type, :disposition => 'attachment', :url_based_filename => true) end end 

Check out the apidock link for more examples.

+9
source

Or you can put an anchor tag:

in your controller:

 @doc = "query to get the document name from your database" @docpath = request.base_url+"/uploads/" +@doc 

in your opinion:

 <a href="@docpath" download>Download File</a>. 
0
source

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


All Articles