简体   繁体   中英

How to use storage to upload a file without active record in Rails 5?

I'm a PHP programmer and I started to work with ROR recently.
In PHP+Laravel there is a quite convenient implementation to upload files, something like

Storage::disk('my_sftp_server')->put('hello.txt', 'Hello World');

https://laravel.com/docs/5.7/filesystem
Where my_sftp_server was set in the Laravel version of the storage.yml file.

As Laravel is strongly based on Rails, I think there should be a similar way to do it with Rails. But when I search for Rails Storage in google, all the results are related to ActiveStorage, and in my case my files are not directly related to my active records.

Is there some way to use the already set Rails storage to upload a file?

Reason:

I'm developing a shipping system and in the end of the day I need to create a consolidated report and save it in the carrier server using SFTP.

您可以将params项写入磁盘:

IO.copy_stream(params[:files][0].tempfile, local_filepath)

What you asked is not good approach. Reason is simple, whenever you store in external storage you have to define various parameter like permission, keys etc.

Let me give you example, You are storing user documents for now, later you are storing different documents, image relevant to different object. Even if you deny to provide such relevance, Rails is fully based on object oriented programming. So you have to give object reference for whatever document you are storing, so you can retrieve it properly.

Paperclip is one of the gem used for it.

To store it in local app path,

 @user.document.copy_to_local_file(nil, path)

Here in above document can be non-db field for user made by attr_accessor . copy_to_local_file is paperclip method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM