Pergunta

I have a problem in my web application. All my uploads on AWS S3 work, but in the form below, the file is saved correctly in the database but not on S3.

The form code (views/users/show.html.haml) :

= simple_nested_form_for @user, :url => save_reply_classaction_opponent_user_path(@classaction, @user.opponent, @user), :html => {:multipart => true} do |f|
    = f.simple_fields_for :reply, @reply do |builder|
      = hidden_field_tag :filter_name, @filter_name
      = hidden_field_tag :filter_type, @filter_type

      %dt= builder.label "Statut", required: false
      %dd= builder.input :state, as: :select, label: false, collection: ["Remboursement effectué", "Demande de remboursement acceptée / En cours", "Remboursement partiel", "Demande rejetée"], include_blank: false
      %dt= builder.label "Montant du remboursement", required: false
      %dd= builder.input :redemption_amount, :placeholder => "Montant en euros", as: :string, label: false
      %dt= builder.label "Date du remboursement", required: false
      %dd= builder.input :redemption_date, as: :date, start_year: Time.now.year, end_year: Time.now.year-5, discard_day: false, order: [:day, :month, :year], label: false
      %dt= builder.label "Justificatif", required: false
      %dd= builder.input :redemption_proof, as: :file, label: false
        - unless @reply.redemption_proof_file_name.nil?
          %dd= link_to(@reply.redemption_proof_file_name, @reply.redemption_proof.url, :target => "_blank")
      %dt= builder.label "Commentaires", required: false
      %dd= builder.input :note, as: :text, label: false

    .clearfix
    = f.submit "Valider la réponse", :class => "le-button button-2"

The Model "Reply" code (models/reply.rb) :

# encoding: utf-8
class Reply
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Paperclip
  include Mongoid::MultiParameterAttributes

  field :state, type: String
  field :redemption_amount
  field :redemption_date, type: Date
  field :note, type: String

  has_mongoid_attached_file :redemption_proof,
    :storage => :s3,
    :path => "/redemption_proofs/:hash.:extension",
    :hash_secret => "jojgHojKbjspvuufRsey78jkHj278",
    :s3_credentials => File.join(Rails.root, 'config', 's3.yml')

  embedded_in :user, inverse_of: :reply

end

The Model "User" code (models/user.rb) :

# encoding: utf-8

class User

  include Mongoid::Document
  include Mongoid::Timestamps

  field :first_name
  field :last_name
  field :email
  field :token
  field :disabled_at, type: DateTime

  embedded_in   :opponent, :inverse_of => :users
  embeds_one    :reply

  accepts_nested_attributes_for :reply

end

The User controller (controllers/users_controller.rb) :

def show
  if @user.reply.nil?
    @reply = @user.build_reply
  else
    @reply = @user.reply
  end
end

def save_reply
  @filter_name = params[:filter_name]
  @filter_type = params[:filter_type]
  if @user.update_attributes(params[:user])
    redirect_to classaction_opponent_user_path(@classaction, @opponent, @user, :moderation => '1', :filter_name => @filter_name, :filter_type => @filter_type)
  end
end

Below that's i have in my console after form validation :

Started PUT "/action-de-groupe/commandes-non-livrees/opponents/52d3e7622c1bb94852000009/users/52dd20d52c1bb96e6800000d/save_reply" for 127.0.0.1 at 2014-02-28 15:01:01 +0100

Processing by UsersController#save_reply as HTML

Parameters: {"utf8"=>"✓", "authenticity_token"=>"QHTxNr27NMBsbND66Rfwde/cPMV06d9T6WEIjIPPagc=", "filter_name"=>"", "filter_type"=>"with_profile", "user"=>{"reply_attributes"=>{"state"=>"Remboursement effectué", "redemption_amount"=>"768", "redemption_date(3i)"=>"24", "redemption_date(2i)"=>"2", "redemption_date(1i)"=>"2014", "redemption_proof"=>#>, "note"=>"test commentaire", "id"=>"530b6c4b2c1bb9cc5e000008"}}, "commit"=>"Valider la réponse", "classaction_id"=>"commandes-non-livrees", "opponent_id"=>"52d3e7622c1bb94852000009", "id"=>"52dd20d52c1bb96e6800000d"}

MOPED: 127.0.0.1:27017 UPDATE
database=class_actions_development collection=classactions selector={"_id"=>"52d3e7622c1bb94852000008"} update={"$set"=>{"opponents.0.users.12.updated_at"=>2014-02-28 14:01:01 UTC, "opponents.0.users.12.reply.redemption_proof_file_name"=>"barbeuk.jpg", "opponents.0.users.12.reply.redemption_proof_content_type"=>"image/jpeg", "opponents.0.users.12.reply.redemption_proof_file_size"=>117083, "opponents.0.users.12.reply.redemption_proof_updated_at"=>2014-02-28 14:01:01 UTC, "opponents.0.users.12.reply.note"=>"test commentaire"}} flags=[] (0.1080ms)

Below that's i have in the database for the document "user" :

"reply" : { "_id" : ObjectId("530b6c4b2c1bb9cc5e000008"), "note" : "test commentaire", "redemption_amount" : "768", "redemption_date" : ISODate("2014-02-24T00:00:00Z"), "redemption_date(1i)" : "2014", "redemption_date(2i)" : "2", "redemption_date(3i)" : "24", "redemption_proof_content_type" : "image/jpeg", "redemption_proof_file_name" : "barbeuk.jpg", "redemption_proof_file_size" : 117083, "redemption_proof_updated_at" : ISODate("2014-02-28T14:01:01.646Z"), "state" : "Remboursement effectué" }

Too, the logs on Heroku while the upload :

2014-02-27T13:31:43.810211+00:00 app[web.1]: [AWS S3 404 0.11337 0 retries] head_object(:bucket_name=>"classactions_production",:key=>"redemption_proofs/530cdb6074581998b500002a/original.jpg") AWS::S3::Errors::NoSuchKey No Such Key

Thanks for your help ! I hope this case will help someone...

Foi útil?

Solução

Perhaps this could be the problem:

s3_credentials: File.join(Rails.root, 'config', 's3.yml')

I would highly recommend setting the s3 credentials in your ENV variables, preferably using the Figaro gem:

#cmd
rails generate figaro:install

#config/application.yml
S3_ACCESS_KEY_ID: "your_key"
S3_SECRET_ACCESS_KEY: "your_key"

#app/models/image.rb
has_mongoid_attached_file :redemption_proof,
    storage: :s3,
    path: "/redemption_proofs/:hash.:extension",
    has_secret: "jojgHojKbjspvuufRsey78jkHj278",
    s3_credentials: {
       access_key_id: ENV['S3_ACCESS_KEY_ID'],
       secret_access_key: ENV['S3_SECRET_ACCESS_KEY']
    }

#cmd
rake figaro:herkoku
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top