0

我正在使用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

新的.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
4

1 回答 1

0

我意识到我在 Restaurant 模型中有一个类型。

mount_uploader :菜单,ImageUploader

应该:

mount_uploader :菜单,MenuUploader

于 2013-06-24T04:07:09.840 回答