背景:我使用的是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'