Paperclip Validation Failed File Name Must Be Set
Background: I'm using Rails 3.0.3, MySQL, and Ruby 1.9.2-p136.
Gems relevant:
gem 'rails', '3.0.3'
gem 'mysql2' gem 'jquery-rails', '>= 1.0.3' gem '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 'paperclip', '~> 2.3', :git => 'git://github.com/thoughtbot/paperclip.git' gem 'cancan' gem 'devise' gem 'simple_form' gem 'kaminari'
Problem: I'm trying to upload a picture via Paperclip to the Amazon S3 server and it won't pass validations. Specifically the message I get is:
Validation failed: Photo file name must be set
If someone could help tell me where I've gone wrong it would be much appreciated.
Here is some of my source code.
Model: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
Controller:
class TransactionsController < ApplicationController
def confirm
end
def create
@transaction = Transaction.new(params[:transaction])
if @transaction.save!
render 'confirm'
else
redirect_to :back
end
end
View:
<%= 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
I know that when I say:
@transaction.save!
it will throw an error message to the browser. If I don't put that, then it won't save and I'll be redirected back to the form. I've read through a lot of tuto开发者_运维知识库rials and I've actually done this before so I'm really confused as to why it won't work. I tried uploading it to Heroku and seeing if it would work there, but it failed as well.
Thanks, any comments are appreciated.
Edit: Here is the message I get from my server.
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'
i think you are facing this problem because you are using RAILS_ROOT which has been deprecated in Rails 3. try using Rails.root instead and see if you face the same problem. Please check the correct usage for Rails.root before making the correction.
精彩评论