背景:我使用的是Rails 3.0.3,MySQL和Ruby 1.9.2-p136。回形针验证失败文件名必须设置
宝石相关:
宝石 '轨道', '3.0.3'
宝石 'mysql2'
宝石 'jquery的护栏', '> = 1.0.3'
宝石'client_side_validations'
宝石 'Rails3中-jQuery的自动填充'
宝石 '耙', '〜> 0.8.7'
宝石 “query_reviewer”:GIT中=>“GIT中://github.com/nesquena/query_reviewer .git“
gem'aws-s3',:require =>'aws/s3'
gem'paperclip','〜> 2.3',::git =>'git://github.com/thoughtbot/paperclip.git'
宝石“康康舞”
宝石“设计”
宝石“simple_form”
宝石“雷”
问题:我试图通过回形针上传图片到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'