必须设置回形针验证失败的文件名

时间:2011-07-11 01:47:49

标签: ruby-on-rails-3 amazon-s3 paperclip

背景:我正在使用Rails 3.0.3,MySQL和Ruby 1.9.2-p136。

宝石相关

  

gem'rail','3.0.3'
   宝石'mysql2'
   gem'jquery-rails','> = 1.0.3'
   gem'client_side_validations'
   gem'trail3-jquery-autocomplete'
   宝石'耙','〜> 0.8.7'
   gem“query_reviewer”,:git => “混帐://github.com/nesquena/query_reviewer.git”
   gem'aws-s3',:require => 'AWS / S3'
   宝石'回形针','〜> 2.3',:git => '混帐://github.com/thoughtbot/paperclip.git'
   gem'cancan'
   宝石'设计'    gem'simple_form'
   宝石'kaminari'

问题:我正在尝试通过Paperclip将图片上传到Amazon S3服务器,但它不会通过验证。具体来说,我得到的信息是:

  

验证失败:必须设置照片文件名

如果有人可以帮我告诉我哪里出错了,我们将不胜感激。

以下是我的一些源代码 型号:

Class Transaction < ActiveRecord::Base

attr_accessible :amount, :user_id, :group_id, :photo

belongs_to :users
belongs_to :groups

has_attached_file :photo, 
   :styles => {
   :thumb=> "100x100#",
   :small  => "200x200>",
   :large => "600x400>" },
 :storage => :s3,
 :s3_credentials => "#{Rails.root.to_s}/config/s3.yml",
 :path => "/:attachment/:id/:style.:extension"

validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 5.megabytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png', 'image/jpg']
end  

控制器:

class TransactionsController < ApplicationController

def confirm
end

def create
@transaction = Transaction.new(params[:transaction])
if @transaction.save!
  render 'confirm'
else
  redirect_to :back
end

end

查看:

<%= form_for(@transaction, :url => transactions_path, :html => { :mulitpart => true }, :validate => true) do |t| %>
<%= t.hidden_field :user_id, :value => current_user.id %>
<%= t.label "Select Group:" %>
<%= t.select :group_id, options_from_collection_for_select(@groups, :id, :name), :class => "ui-corner-all" %><br />
<%= t.label "Amount:" %>
$ <%= t.text_field :amount, :size => 20 %> <br />
<%= t.label "Receipt:" %>
<%= t.file_field :photo, :size => 20 %> <br />
<%= t.submit 'Continue', :name => "withdrawal" %>
<% end %>  

配置/ s3.yml:

development:
bucket: trans-dev
access_key_id: MY_ID
secret_access_key: MY_KEY
test:
bucket: trans-test
access_key_id: MY_ID
secret_access_key: MY_KEY
production:
bucket: trans-pro
access_key_id: MY_ID
secret_access_key: MY_KEY  

schema.rb:

create_table "transactions", :force => true do |t|
t.integer  "amount",             :default => 0,     :null => false
t.integer  "user_id",                               :null => false
t.integer  "group_id",                              :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.string   "photo_file_name"
t.string   "photo_content_type"
t.integer  "photo_file_size"
t.datetime "photo_updated_at"
end

我知道,当我说:

  

@ transaction.save!

它会向浏览器抛出错误消息。如果我不把它,那么它将不会保存,我将被重定向回到表单。我已经阅读了很多教程,之前我实际上已经这样做了,所以我真的很困惑为什么它不起作用。我尝试将它上传到Heroku并查看它是否可以在那里工作,但它也失败了。

谢谢,任何评论都表示赞赏。

编辑:这是我从服务器获得的消息。

Started POST "/transactions" for 127.0.0.1 at 2011-07-12 18:25:12 -0400
Processing by TransactionsController#create as HTML
Parameters: {"utf8"=>"✓",       
"authenticity_token"=>"y8hAJq7+SGP4qEcWOBz/hmlIzgeCgEuqayY5EluMt18=", "transaction"=> 
{"user_id"=>"7", "group_id"=>"2", "amount"=>"3.25", "description"=>"plz work!", 
"purpose"=>"0", "item_category"=>"0", "photo"=>"Ctrl Alt Del.jpeg"}, "countdown"=>"91", 
"withdrawal"=>"Continue"}
Group Load (0.3ms)  SELECT SQL_NO_CACHE `groups`.* FROM `groups` WHERE (`groups`.`id` = 2) LIMIT 1
SQL (0.2ms)  BEGIN
SQL (0.2ms)  ROLLBACK
Completed   in 143ms

ActiveRecord::RecordInvalid (Validation failed: Photo file name must be set):
app/controllers/transactions_controller.rb:15:in `create'

1 个答案:

答案 0 :(得分:1)

我认为您正面临这个问题,因为您正在使用已在Rails 3中弃用的RAILS_ROOT。请尝试使用Rails.root,看看您是否面临同样的问题。在进行更正之前,请检查Rails.root的正确用法。