我正在使用carrierwave将文件/图像上传到我的应用程序。我通过Amazon AWS存储文件。上传适用于图像,但在尝试添加.PDF时,我在服务器日志中收到“回滚事务”错误,我不确定原因。
Started POST "/restaurants" for 127.0.0.1 at 2013-06-21 14:39:16 -0400
Processing by RestaurantsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"filAk5Jh++qmb7owXVgYin2C4nq2O+09gMeQV27gKeU=", "restaurant"=>{"name"=>"The Stumble Inn", "address"=>"", "phone"=>"", "description"=>"", "image"=>#<ActionDispatch::Http::UploadedFile:0x00000101da3f98 @original_filename="stumble.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"restaurant[image]\"; filename=\"stumble.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/var/folders/6b/tq59gs0d1f7bp_zg41cf6fqm0000gn/T/RackMultipart20130621-40432-124zf2w>>, "remote_image_url"=>"", "menu"=>#<ActionDispatch::Http::UploadedFile:0x00000101da3d18 @original_filename="menu.pdf", @content_type="application/pdf", @headers="Content-Disposition: form-data; name=\"restaurant[menu]\"; filename=\"menu.pdf\"\r\nContent-Type: application/pdf\r\n", @tempfile=#<File:/var/folders/6b/tq59gs0d1f7bp_zg41cf6fqm0000gn/T/RackMultipart20130621-40432-3cwvjc>>}, "commit"=>"Create Restaurant"}
(0.2ms) begin transaction
(0.2ms) rollback transaction
Redirected to http://localhost:3000/restaurants
class MenuUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
# Choose what kind of storage to use for this uploader:
storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
class Restaurant < ActiveRecord::Base
attr_accessible :address, :description, :name, :phone, :image, :remote_image_url, :menu
mount_uploader :image, ImageUploader
mount_uploader :menu, ImageUploader
end
new.html.erb
<h1>Add new restaurant</h1>
<%= form_for(@restaurant, :html => {:multipart => true}) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :address %>
<%= f.text_field :address %>
<%= f.label :phone %>
<%= f.text_field :phone %>
<%= f.label :description %>
<%= f.text_field :description %>
<%= f.label :upload_image %>
<%= f.file_field :image %>
<%= f.label :remote_image_url, "or image URL:" %>
<%= f.text_field :remote_image_url %>
<%= f.label :upload_menu %>
<%= f.file_field :menu %><br/><br/>
<%= f.submit "Create Restaurant" %>
<% end %>
class RestaurantsController < ApplicationController
def new
@restaurant = Restaurant.new
end
def create
@restaurant = Restaurant.new(params[:restaurant])
@restaurant.save
redirect_to restaurants_path
end
答案 0 :(得分:0)
我意识到我在餐厅模型中有一种类型。
mount_uploader:menu,ImageUploader
应该是:
mount_uploader:menu,MenuUploader