使用Multipart Form在Ruby on Rails 3上无法使用文件上传

我有一个非常简单的配置来上传文件。 我只想将图像上传到数据库(我知道,我知道……)并附上说明。 而已。

这是我的模型的样子(我从这里学到的)。 基本上,它正是设置示例的方式,只有attr_protected行才能使用Rails 3.2:

 Photo < ActiveRecord::Base # note: there is also a :description attribute!! attr_protected :file_name, :content_type, :binary_data def image_file=(input_data) self.file_name input_data.original_filename self.content_type = input_data.content_type.chomp self.binary_data = input_data.read end end 

这是我非常简单的视图:

 

New Photo

Upload a Photo

最后(仅为完整性),这是我的Create动作(简单的脚手架):

 def create @photo = Photo.new(params[:photo]) respond_to do |format| if @photo.save format.html { redirect_to @photo, notice: 'Photo was successfully created.' } format.json { render json: @photo, status: :created, location: @photo } else format.html { render action: "new" } format.json { render json: @photo.errors, status: :unprocessable_entity } end end end 

这个想法是用户将选择一个文件并输入一个小描述,该描述应该发布到/photos控制器上的’Create’方法。 不幸的是,当我点击提交按钮时,我得到一个非常模糊的错误,但没有提供有关真正问题的信息:

 ArgumentError in PhotosController#create wrong number of arguments (1 for 0) Rails.root: /Users/me/dev/photo-app Application Trace | Framework Trace | Full Trace app/models/photo.rb:4:in `image_file=' app/controllers/photos_controller.rb:43:in `new' app/controllers/photos_controller.rb:43:in `create' Request Parameters: {"utf8"=>"✓", "authenticity_token"=>"qSjMHgjCDo2ORmTvnujrljfPCj6oekLI9KAz4x5gA7Q=", "photo"=>{"image_file"=>#<ActionDispatch::Http::UploadedFile:0x00000101726fd0 @original_filename="SomePic.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"photo[image_file]\"; filename=\"SomePic.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#>, "description"=>"my description"}, "commit"=>"Upload"} 

我看过谷歌和StackOverflow,但似乎没有什么能比得上(非常简单)的例子。 任何帮助将不胜感激!

提前致谢!

这里,image_file只包含ActionDispatch对象,而不包含文件的内容和描述,如下所示:

 "image_file" => # 

这看起来很荒谬,无法上传文件。 无论哪种方式,您都必须通过阅读:tempfile将内容写入上传位置

我接受了@kiddorails的建议并实施了CarrierWave和Amazon Web Service 。 我的项目需要图片上传,我只限于Heroku。 因此,我潜入并想通了。 在后面看来,它是最好的解决方案,但当时对我的(样本)项目应用程序来说似乎有点过头了。 这对我来说很棘手(作为’非Ruby on Rails’开发人员)。

在一天结束时,我会推荐任何希望将图像存储到数据库中的人,而不是那样做,并做同样的事情。 学习正确的方法。 🙂