マルチパート形式を使用してiPhoneからRailsに写真を送信できたと思いますが、写真がRailsサーバーに届くと、正しく保存する方法がわかりません。このファイルは、iphone_photo_to_websiteが呼び出されたときに、以下のパラメーターの最後にある「userfile」としてRailsサーバーのパラメーターに表示されると思います。
Parameters: {"action"=>"iphone_photo_to_website","id"=>"8A39FA8F_1ADD_52AB_8387_E1C5CFC4AB2D", "controller"=>"users", "userfile"=>#<File:/tmp/RackMultipart30053-0>}
次のSO質問から次のObjective-Cコードを使用してiPhoneから写真を送信します。
iPhoneを搭載したサーバーに写真をアップロードするにはどうすればよいですか?
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"logo1" ofType:@"png"];
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
NSString *urlString = [NSString stringWithFormat:@"%@/users/iphone_photo_to_website/%@",
serverString, UID];
NSMutableURLRequest *imageRequest = [[NSMutableURLRequest alloc] init] ;
[imageRequest setURL:[NSURL URLWithString:urlString]];
[imageRequest setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[imageRequest setValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData dataWithCapacity:[imageData length] + 512];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"logo1.png\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[imageRequest setHTTPBody:body];
theConnection = [[NSURLConnection alloc] initWithRequest:imageRequest
delegate:self];
Railsサーバーでは、db/migrateでの写真の移行は次のようになります。
class CreatePhotos < ActiveRecord::Migration
def self.up
create_table :photos do |t|
t.column :user_id, :integer
t.column :title, :string
t.column :body, :text
t.column :created_at, :datetime
# the following are for attachment_fu
t.column :photo, :binary
t.column :content_type, :string
t.column :filename, :string
t.column :size, :integer
t.column :parent_id, :integer
t.column :thumbnail, :string
t.column :width, :integer
t.column :height, :integer
end
end
def self.down
drop_table :photos
end
end
Railsサーバーには、次のコードの写真モデルapp / models/photo.rbがあります。
class Photo < ActiveRecord::Base
has_attachment :storage => :file_system,
:resize_to => '640x480',
:thumbnails => { :thumb => '160x120', :tiny => '50>' },
:max_size => 5.megabytes,
:content_type => :image,
:processor => 'Rmagick'
validates_as_attachment
belongs_to :user
end
また、Railsコントローラーには次のコードがあります。
def iphone_photo_to_website
@udid = params[:id]
@userfile = params[:userfile]
@photo_params = { :user_id => @user.id,
:photo => @userfile,
:content_type => 'image',
:filename => @userfile.original_path,
:uploaded_data => @userfile
}
image = Photo.new(@photo_params)
image.save
end
「image.save」を実行すると、「false」が返されます。image.saveがfalseを返す原因となっているエラーを見つける方法を知っている人はいますか?または、写真をデータベースに正しく保存する方法を知っている人はいますか?