使用ASIHTTPRequest从iPhone上传到Ruby on Rails

8
我很需要帮助!我正在开发一个应用程序,它与我的ROR Web服务器通信以进行数据库请求。由于ActiveResource的帮助,它运行得非常好。但现在我还需要上传文件到服务器,并计划使用ASIHTTPRequest。虽然ASIHTTPRequest看起来很不错,但我的问题是我不确定如何在ROR端处理POST请求... 我正在使用paperclip,但真的遇到了瓶颈。
在ASIHTTP方面,我只需编写:
[request setData:data withFileName:@"photo.jpg" andContentType:@"image/jpeg" forKey:@"asset[image]"];

在 Ruby 方面,我正在做...

class Asset < ActiveRecord::Base
   validates_attachment_presence :image
    has_attached_file :image
end

class AssetsController < ApplicationController  
    protect_from_forgery :only => [:update, :destroy] 
.....

但它总是失败,我非常确定这与POST表单数据集有关,但我完全卡住了。
我收到了以下错误信息:
 Parameters: {"assets"=>{"images"=>#<File:/var/folders/gM/gM15qjM2G3W0iVNaT1evD++++TI/-Tmp-/RackMultipart20091112-2285-2i0qq5-0>}}

NoMethodError (You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]):
  app/models/asset.rb:2
  app/controllers/assets_controller.rb:46:in

`create'

任何帮助都将非常感激地接受。Chris,谢谢!
1个回答

1

在上传时,我首先要检查的是将参数名称设置为file_column(或任何其他名称,个人建议使用Paperclip)所期望的名称。

如果你有类似这样的代码:

class Entry < ActiveRecord::Base
    file_column :image
end

您需要确保参数(表单字段名称)与预期相对应。对于上面的示例,这将是:

name="entry[image]"

同时,请确保您正在进行多部分表单提交,而不仅仅是标准提交。


1
我还发现你需要包含[request setPostValue:@"Create" forKey:@"commit"]; - Chris

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接