【问题标题】:Paperclip Validation Failed File Name Must Be Set回形针验证失败文件名必须设置
【发布时间】:2011-07-11 01:47:48
【问题描述】:

背景:我使用的是 Rails 3.0.3、MySQL 和 Ruby 1.9.2-p136。

宝石相关

宝石'rails','3.0.3'
宝石'mysql2'
gem 'jquery-rails', '>= 1.0.3'
宝石'client_side_validations'
gem 'rails3-jquery-autocomplete'
gem 'rake', '~> 0.8.7'
gem "query_reviewer", :git => "git://github.com/nesquena/query_reviewer.git"
gem 'aws-s3', :require => 'aws/s3'
gem '回形针', '~> 2.3', :git => 'git://github.com/thoughtbot/paperclip.git'
宝石“康康”
宝石“设计”
宝石'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 %>  

config/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 并查看它是否可以在那里工作,但它也失败了。

谢谢,感谢任何 cmets。

编辑:这是我从服务器收到的消息。

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'

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-10
      相关资源
      最近更新 更多