简体   繁体   English

在Ruby on Rails应用程序中向数据库添加文件吗?

[英]Add a file to a database in a Ruby on Rails application?

I've only just started learning ruby on rails and I would like to create an application that will allow me to add files to the database. 我才刚刚开始学习Rails上的ruby,我想创建一个应用程序,允许我向数据库中添加文件。 Currently, I'm developing the rails application using the Aptana plugin for Eclipse and the application is using the default sqllite db. 目前,我正在使用用于Eclipse的Aptana插件开发rails应用程序,并且该应用程序正在使用默认的sqllite db。

I have tried generating a scaffold with the following parameters: documents title:string file:varbinary . 我尝试生成具有以下参数的支架: documents title:string file:varbinary Then I do a 'rake'->'db'->'migrate'. 然后我做一个'rake'->'db'->'migrate'。 When I migrate to localhost/documents and click on 'New Document' the application fails and displays an error. 当我迁移到localhost / documents并单击“ New Document”时,应用程序失败并显示错误。

What I would like to do is click on 'New Document', have a field that will allow me to browse for a document on my local computer, select the document and add it to the db on the rails application. 我想做的是单击“新建文档”,有一个字段可以让我浏览本地计算机上的文档,选择该文档并将其添加到rails应用程序的数据库中。

Paperclip is more recommended than attachment_fu these days. 最近,比起attachment_fu,更推荐使用回形针 It is really simple and easy to use with your active record model. 它与活动记录模型一起使用非常简单容易。

Is it a particular kind of file you want to add? 您要添加的特定文件类型吗?

I just ask because if it's not data of a kind that benefits from being in a database ( textual data might be searchable, binary data is not ) then you are much better storing it in the filesystem and serving it up straight - especially for stuff like images or video - rather than inserting it into a database and having to go through your application every time a user requests it. 我只是问,因为如果不是那种可以从数据库中受益的数据(文本数据可能是可搜索的,二进制数据则不是),那么最好将其存储在文件系统中并直接提供服务-特别是对于诸如图片或视频-而不是将其插入数据库,并且每次用户请求时都必须遍历您的应用程序。

I'm not saying that there aren't any reasons you might want to have a file in the database, but I treat that as a last resort and in ten years of web programming I've not come across a case where it was necessary. 我并不是说您可能没有任何理由要在数据库中保存文件,但是我认为这是不得已而为之的,在十年的Web编程中,我没有遇到过必要的情况。

I would highly recommend the attachment_fu plugin as this lets you create models with attachments pretty nicely, Paperclip plugin is another good one also! 我强烈推荐附件_fu插件,因为它可以让您很好地创建带有附件的模型,Paperclip插件也是另一个不错的选择!

If you have trouble deciding which one to use, as far as i can remember, Paperclip makes it easier for multiple attachments, such as an Album has many Photos, and Attachment_fu is easier for single attachments such as a User has one display picture. 据我所记得,如果您在决定使用哪个附件时遇到麻烦,Paperclip可以使多个附件(例如相册中有很多照片)变得更容易,而Attachment_fu对于单个附件(如用户中只有一张显示图片)则更容易。

We do something like this on a site I'm managing. 我们在我管理的网站上执行类似操作。 Instead of storing these files in a database, I'd agree with the other posters here and recommend you try something like Paperclip. 除了将这些文件存储在数据库中之外,我将与此处的其他张贴者保持一致并建议您尝试使用Paperclip之类的工具。

One caveat: if you want access control, make sure that paperclip doesn't save your files somewhere under /public, where anyone could possibly access them if they knew the URL. 一个警告:如果要访问控制,请确保回形针不会将文件保存在/ public下的某个位置,如果知道URL,则任何人都可以访问它们。 Deliver files to the user via send_file in your controller. 通过控制器中的send_file将文件传递给用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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