0

I'm receiving the following error:

The AWS Access Key Id you provided does not exist in our records.

I'm not sure why I would be getting this error because I have the correct access key and secret key shown to me in my aws security credentials page (I have copied and pasted multiple times to refute the possibility of a typo). Why would it be telling me that my access key is not in their records if I am able to log in and visit my STILL empty buckets?

Here is my s3.yml:

GMAIL_USERNAME: <my email>
GMAIL_PASSWORD: <my password>
MONGOHQ_URL: <my mongoid url>
S3_BUCKET_NAME_DEVELOPMENT: <my development bucket>
S3_BUCKET_NAME_PRODUCTION: <my production bucket>
S3_BUCKET_NAME_TEST: <my test bucket>
AWS_ACCESS_KEY_ID: <my access key>
AWS_SECRET_ACCESS_KEY: <my secret key>

Here is my picture.rb:

class Picture
  include Mongoid::Document
  include Mongoid::Paperclip

  embedded_in :datum, inverse_of: :pictures

  has_mongoid_attached_file :attachment,
                            path: ':attachment/:id/:style.:extension',
                            storage: :s3,
                            url: ':s3_domain_url',
                            bucket: Proc.new{ |a| a.instance.s3_bucket },
                            s3_credentials: File.join(Rails.root, 'config', 's3.yml'),
                            styles: {
                                original: ['1920x1680', :jpg],
                                small: ['100x100', :jpg],
                                medium: ['250x250', :jpg],
                                large: ['500x500', :jpg]
                            },
                            convert_options: { all: '-background white -flatten +matte' }

  def s3_bucket
    if Rails.env.development?
      ENV['S3_BUCKET_NAME_DEVELOPMENT']
    else
      ENV['S3_BUCKET_NAME_PRODUCTION']
    end
  end
end

Here is my datum.rb:

class Datum
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Paperclip

  field :title, type: String
  field :user_id, type: Integer

  validates :title, presence: true
  validates :user_id, presence: true

  embeds_many :todo_items
  embeds_many :pictures, cascade_callbacks: true
  accepts_nested_attributes_for :pictures
end

Here is my datum_params method:

# Never trust parameters from the scary internet, only allow the white list through.
def datum_params
  params.require(:datum).permit(:title, :user_id, :identifying_image, pictures_attributes: [:picture])
end

Here is my picture_params method:

def picture_params
  params.require(:picture).permit(:datum_id, :attachment)
end

Here is the form I use for the upload:

<%= bootstrap_form_for(picture, url: { action: :create }, html: { multipart: true }, remote: true ) do |f| %>
  <%= f.file_field :picture, multiple: true, name: 'picture[picture]' %>
  <%= f.submit 'Upload' %>
<% end %>

Originally, I was having a strong_parameters issue that rails4guides.com (the user with the answer below) was able to help me overcome. I knew I made progress when I stopped getting an unpermitted parameter error and started getting errors from AWS. Can anyone see any reason why my credentials are not working? Am I supposed to be seeing anything about my access key in the request parameters? Because I don't at this time.

Jake Smith
  • 2,332
  • 1
  • 30
  • 68
  • 1
    In your datum_params it should be pictures_attributes (plural). In your picture_params you should have the name of the attachment whitelisted. Weird enough, it seems to expect picture altough it seems to me it is named attachment. Still I would try params.require(:picture).permit(:picture). The last :picture refers to the name of the paperclip attachment. – rails4guides.com Apr 04 '14 at 08:07
  • Thanks for the suggestion! I'll be able to try these after work today, but one thing I can say is that if I try `params.require(:picture).permit(:picture)` it complains about the `Picture` class not having a method called `picture`. If I'm trying to use the `jquery-fileupload-rails` gem to give me the ability to upload more than one file at a time, do I need to do anything more about `picture` vs. `pictures`? – Jake Smith Apr 04 '14 at 15:37
  • 1
    Well, with paperclip you normally permit the name of your attachment. In your case you would expect :attachment. With regard to nested attributes, the reference to it in your permitted params should always be plural. P.s. Did you also get the exact same error before using 'pictures_attribute: [:picture]' ? – rails4guides.com Apr 04 '14 at 18:48
  • No I didn't. It created the picture, but never gave me a reason why things weren't getting pushed through to s3. The only time I got the no method error was when I had `params.require(:picture).permit(:picture)`. I'll change that to `params.require(:picture).permit(:attachment)` and change the datum_params method body to `params.require(:datum).permit(..., pictures_attributes: [:pictures])`. Is that in line with what you are suggesting? I can get you the results of these changes around 6pm U.S. Mountain Time. I really appreciate your help thus far. – Jake Smith Apr 04 '14 at 18:51
  • If this doesn't point me in the direction to solving this, what other information would be useful for me to add to my question? I'm going to add the form I use for the file_field as well... – Jake Smith Apr 04 '14 at 18:52

1 Answers1

1

Okay, I will explain further in a separate answer to make further elaboration more clear.

In a typical situation you would have something like this in your form :

<%= f.file_field :picture %>

Picture is the name of the file you want to upload. Now imagine this form will pass on it's results to the ImagesController. In order for the ImagesController to be able to process the picture it should be whitelisted through strong parameters, as in:

def image_params
  params.require(:image).require(:picture)
end

An other case would be using nested attributes. If you want to set a picture for an article your form will look something like this:

<% f.fields_for :image do |image_form| %>
  <%= image_form.file_field :picture %>
<% end %>

And your article_params will look like this:

def article_params
  params.require(:article).permit(:title, :content, images_attributes: [:picture])
end

Small note: It should be images_attributes (both plural) and not image_attributes. The actual nested attributes on the other hand should be singular.

This is pretty much a basic outline of strong parameter usage, I hope it will help you. P.s. There might be some typos, since I'm typing this on my mobile phone.

rails4guides.com
  • 1,441
  • 2
  • 11
  • 8
  • Awesome! Thank you! So the nested attributes `fields_for` block would only be necessary if I had this inside my `form_for @article ...` correct? If I have the form for uploading the picture show up in a modal that is outside the `form_for @artcile ...`, then I wouldn't need to use `fields_for` right? – Jake Smith Apr 04 '14 at 20:32
  • your suggestions got me closer! I'm now getting AWS errors! Which I think is an improvement! It is saying that the access key I'm giving is not in their records...I have gone to the credentials page of aws and copied and pasted my access key, secret key and bucket names from the site. I'm not sure what else I can do to solve this issue. Any ideas? – Jake Smith Apr 05 '14 at 02:44
  • First comment: That is right, in your case you don't need nested attributes. Second comment: This no longer seems related to strong parameters, I would check github issues with the particular aws error you are receiving. – rails4guides.com Apr 05 '14 at 05:55
  • No luck there so far, that's where I've been looking. But I shall not give up! I have already updated my question to reflect the recent developments. – Jake Smith Apr 05 '14 at 08:35
  • It was not recognizing my s3.yml file. I had to change the `s3_credentials: ...` to `s3_credentials: { access_key_id: <...>, secret_access_key: <...> }`. Then it started to work locally :) Thank you so much for your help! – Jake Smith Apr 05 '14 at 08:55
  • 1
    Good to hear. In case you are facing troubles concerning strong parameters in the future, I have just written an article about it: http://www.rails4guides.com/admin/articles/strong-parameters-in-action – rails4guides.com Apr 05 '14 at 10:58