简体   繁体   中英

Rails- To save the URL or the file?

I am new to webdevelopment, and to rails, and I am writing an app to build galleries, but I did see 2 different ways to do it, some take the file and save it to some folder in the server, and save some reference in the DB (not sure if they keep the url to the file in the filesystem or just the name and find it some way), or they save it as a binary directly to the DB.

my question is: which way is better?, why to do it one way instead of the other or what would be the "rule" to do it x way?

if you know some other method(s) I would appreciate it if you can write them here.

Great question. Here's a reason to not save files directly to the server:

In the not-too-distant future, you may have the need to host your site on multiple servers. If a file is on one of them, the other servers will not have easy access to it. Therefore, you'll want to save your pictures to some central location.

As with most static assets, the ideal place to store them is on a Content Delivery Network (CDN). This means your files are on the cloud, and can be accessed easily and quickly by all your servers. So you'll end up storing a filename in the database, and your HTML will use that filename to find the correct file that's stored on the CDN. Gems like Paperclip and Carrierwave let you do this, and this is the most popular way (with Rails) for dealing with uploading images.

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