I have a problem with my avatar upload. It worked once for me but I must be blind this time.
I am using paperclip and aws-sdk gem and set up everything as described here.
I have tested it with 2 buckets. One with the standard us-location and one with the eu-west-1 location. Neither is working.
Here is all my related code:
config/s3.yml
development:
bucket: ***
access_key_id: ***
secret_access_key: ***
test:
bucket: ***
access_key_id: ***
secret_access_key: ***
production:
bucket: ***
access_key_id: ***
secret_access_key: ***
app/model/art.rb
class Art < ActiveRecord::Base
attr_accessible :created_at, :description, :name, :avatar
has_attached_file :avatar,
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:url => ':set_alias_url',
:path => "/:style/:id/:filename.:extension",
:bucket => '***'
end
app/controller/arts_controller.rb
class ArtsController < ApplicationController
...
def create
@art = Art.new(params[:art])
if @art.save
flash[:notice] = "sucessfully saved upload"
redirect_to arts_path
else
flash[:notice] = "error"
render "new"
end
...
app/views/arts/new.html.erb
<h1>Arts#new</h1>
<p>Find me in app/views/arts/new.html.erb</p>
<%= form_for(@art, :url => arts_path, :html => { :multipart => true }) do |f| %>
<%= f.label :name %> <br/>
<%= f.text_field :name %> <br/>
<%= f.label :description %> <br/>
<%= f.text_field :description %> <br/>
<%= f.label :avatar %> <br/>
<%= f.file_field :avatar %> <br/>
<%= f.submit %>
<% end %>
I searched the web for 2 days now but I cannot find my error. Here is the response from the server:
POST "/arts/new" for 127.0.0.1 at 2012-12-19 12:11:39 +0100
Processing by ArtsController#new as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ZyZFfSaTl9CQpL5kOXyt/rcoi+SCuNp5/deCYda83sE=", "art"=>{"name"=>"asdf",
"description"=>"asdf",
"avatar"=>#<ActionDispatch::Http::UploadedFile:0x0000000296e0b8 @original_filename="lakritz10.jpg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"art[avatar]\"; filename=\"lakritz10.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/tmp/RackMultipart20121219-5804-1rnvkmj>>},
"commit"=>"Create Art
So can anyone help me to fix this?
thanks in advance.
EDIT: It seems like even without an attachment that nothing is getting saved in the database. Thats wired…
EDIT_SOLUTION: In order to solve my particular problem I had to change the routes.rb
I previously assumed that the entry
match "arts/new", :to => "arts#new", :as => 'arts'
would do the job. This was not the case.
I had to change the line into:
resources :arts
Because i do not understand how routes exactly work yet, I cannot explain it.
Your line:
only creates a route to the
newaction. It mapsarts_pathtoarts/newwhich is then the target of your form. You really need to submit the form toarts#createwhich is what the resource does automatically for aPOSTwith a new object.