简体   繁体   English

使用paperclip和aws-sdk gem不会上传头像

[英]Avatar is not getting uploaded using paperclip and aws-sdk gem

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. 我正在使用paperclip和aws-sdk gem,并按照此处的描述设置所有内容。

I have tested it with 2 buckets. 我用2个桶测试过它。 One with the standard us-location and one with the eu-west-1 location. 一个是标准的us-location ,另一个是eu-west-1位置。 Neither is working. 两者都不起作用。

Here is all my related code: 这是我所有相关的代码:

config/s3.yml 配置/ 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 应用程序/模型/ 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 应用程序/控制器/ 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 应用程序/视图/艺术/ 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. 我现在在网上搜索了2天,但我找不到我的错误。 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 EDIT_SOLUTION:为了解决我的特殊问题,我不得不改变routes.rb我以前假设的条目

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: 你的路线:

match "arts/new", :to => "arts#new", :as => 'arts'

only creates a route to the new action. 仅创建指向new操作的路径。 It maps arts_path to arts/new which is then the target of your form. 它将arts_path映射到arts/new ,然后将其作为表单的目标。 You really need to submit the form to arts#create which is what the resource does automatically for a POST with a new object. 您确实需要将表单提交给arts#create ,这是资源为具有新对象的POST自动执行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM