简体   繁体   中英

I can't save a book after adding image and resource in Ruby

I need help with this. I am creating a demo book store with ruby and amazon web services. I have installed the gem 'aws-sdk' and everything works fine until I try to create the new book with the image and resource. I get the error listed on the link . "app/controllers/books_controller.rb:29:in `create' " (sorry I added the Full Trace as I thought that would help)If I remove the line of code that it suggests, it works, but the book doesn't show up in the dashboard.

Let me know if you need anything from me. I haven't pushed the latest commits to github because I don't want any errors, but if need be, I will. Here is the latest repo without those features.

Here are the steps I took to get to this point. Maybe I missed ssomething? Add aws-sdk gem to my gemfile

Setup paperclip defaults to AWS S3 and set its credentials

app/config/apllication.rb

config.paperclip_defaults = {
storage: :s3,
s3_credentials: {
bucket: ENV['AWS_BUCKET'],
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
}
} 

Add both attachments(:image/:resource) input fields to the books form

<%= simple_form_for(@book, html: {class: "form-signin"}) do |f| %>
<%= f.error_notification %>


<%= f.input :name, required: true, label: false, placeholder: "Name", input_html: {class: "form-control"} %>
<%= f.input :author, required: true, label: false, placeholder: "Author's name", input_html: {class: "form-control"} %>
<%= f.input :description, required: true, label: false, placeholder: "Description", input_html: {class: "form-control", rows: 5} %>
<br>
<%= f.input :price, required: true, label: false, placeholder: "Price", input_html: {class: "form-control"} %>
<%= f.input :image %>
<%= f.input :resource %>
<div class="checkbox text-center">
<p>Availability</p>
<%= f.input :availability, required: false, label: false %>
</div>
<%= f.button :submit, class: "btn btn-primary btn-block" %>
<% end %> 

Validate the content-type of the attachments as well as their presence

    validates_attachment_content_type :image,
content_type: /^image\/(png|gif|jpeg)/,
message: "Only images allowed"

validates_attachment_content_type :resource,
content_type: ['application/pdf'],
message: "Only pdfs allowed"

validates :image, attachment_presence: true
validates :resource, attachment_presence: true 

Then comes the error when I try to create a new book "books/new"

UPDATE:

Here is my terminal out put when I click "Create Book"

jshuadvd:estuk joshuadavid$ rails s
=> Booting WEBrick
=> Rails 4.1.1 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
=> Ctrl-C to shutdown server
[2015-03-02 21:10:03] INFO  WEBrick 1.3.1
[2015-03-02 21:10:03] INFO  ruby 2.1.2 (2014-05-08) [x86_64-darwin13.0]
[2015-03-02 21:10:03] INFO  WEBrick::HTTPServer#start: pid=30712 port=3000


Started GET "/books/new" for 127.0.0.1 at 2015-03-02 21:10:10 -0800
  ActiveRecord::SchemaMigration Load (0.1ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by BooksController#new as HTML
  User Load (0.2ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 1  ORDER BY "users"."id" ASC LIMIT 1
  Rendered books/_form.html.erb (71.2ms)
  Rendered books/new.html.erb within layouts/application (75.0ms)
  Rendered layouts/_header.html.erb (1.1ms)
Completed 200 OK in 389ms (Views: 354.1ms | ActiveRecord: 1.0ms)


Started POST "/books" for 127.0.0.1 at 2015-03-02 21:10:55 -0800
Processing by BooksController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"9WlfCy70fc+OZdNVq8vXpLGWGB6ifUBffQ4M5E/71XU=", "book"=>{"name"=>"The Book of Ruby", "author"=>"Huw Collingbourne", "description"=>"A Book of Ruby", "price"=>"1299", "image"=>#<ActionDispatch::Http::UploadedFile:0x007fa14a9a7a98 @tempfile=#<Tempfile:/var/folders/1z/dl8cfs4n3b53d8lqr4l8mbfr0000gq/T/RackMultipart20150302-30712-wp2ftx>, @original_filename="ruby_frontcvr.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"book[image]\"; filename=\"ruby_frontcvr.png\"\r\nContent-Type: image/png\r\n">, "resource"=>#<ActionDispatch::Http::UploadedFile:0x007fa14a9a7908 @tempfile=#<Tempfile:/var/folders/1z/dl8cfs4n3b53d8lqr4l8mbfr0000gq/T/RackMultipart20150302-30712-soog2z>, @original_filename="The Book of Ruby.pdf", @content_type="application/pdf", @headers="Content-Disposition: form-data; name=\"book[resource]\"; filename=\"The Book of Ruby.pdf\"\r\nContent-Type: application/pdf\r\n">, "availability"=>"1"}, "commit"=>"Create Book"}
  User Load (0.3ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 1  ORDER BY "users"."id" ASC LIMIT 1
Command :: file -b --mime '/var/folders/1z/dl8cfs4n3b53d8lqr4l8mbfr0000gq/T/cf4baff8e3f9d6dc5ecb7be1577b42fa20150302-30712-1rfuyrq.png'
Command :: file -b --mime '/var/folders/1z/dl8cfs4n3b53d8lqr4l8mbfr0000gq/T/b5e2665470b6cb72f39833d889a4b7f620150302-30712-ghgiut.pdf'
   (0.1ms)  begin transaction
Command :: file -b --mime '/var/folders/1z/dl8cfs4n3b53d8lqr4l8mbfr0000gq/T/cf4baff8e3f9d6dc5ecb7be1577b42fa20150302-30712-x17x01.png'
Command :: file -b --mime '/var/folders/1z/dl8cfs4n3b53d8lqr4l8mbfr0000gq/T/b5e2665470b6cb72f39833d889a4b7f620150302-30712-1pncf55.pdf'
Binary data inserted for `string` type on column `image_content_type`
Binary data inserted for `string` type on column `resource_content_type`
  SQL (0.7ms)  INSERT INTO "books" ("author", "created_at", "description", "image_content_type", "image_file_name", "image_file_size", "image_updated_at", "name", "price", "resource_content_type", "resource_file_name", "resource_file_size", "resource_updated_at", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["author", "Huw Collingbourne"], ["created_at", "2015-03-03 05:10:56.007365"], ["description", "A Book of Ruby"], ["image_content_type", "image/png"], ["image_file_name", "ruby_frontcvr.png"], ["image_file_size", 86035], ["image_updated_at", "2015-03-03 05:10:55.920455"], ["name", "The Book of Ruby"], ["price", 1299], ["resource_content_type", "application/pdf"], ["resource_file_name", "The_Book_of_Ruby.pdf"], ["resource_file_size", 8184750], ["resource_updated_at", "2015-03-03 05:10:55.959655"], ["updated_at", "2015-03-03 05:10:56.007365"], ["user_id", 1]]
[paperclip] saving /books/images/000/000/006/original/ruby_frontcvr.png
[AWS S3 301 0.424533 0 retries] put_object(:acl=>:public_read,:bucket_name=>"jdi.development",:content_length=>86035,:content_type=>"image/png",:data=>Paperclip::UploadedFileAdapter: ruby_frontcvr.png,:key=>"books/images/000/000/006/original/ruby_frontcvr.png") AWS::S3::Errors::PermanentRedirect The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.

   (1.1ms)  rollback transaction
Completed 500 Internal Server Error in 710ms

AWS::S3::Errors::PermanentRedirect (The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.):
  app/controllers/books_controller.rb:29:in `create'


  Rendered /Users/joshuadavid/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.6ms)
  Rendered /Users/joshuadavid/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.3ms)
  Rendered /Users/joshuadavid/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.3ms)
  Rendered /Users/joshuadavid/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (16.4ms)

This is caused by the new aws-sdk gem, change your gemfile to install an older version than 2.0

gem 'aws-sdk', '< 2.0'

and it should work.

For more info http://ruby.awsblog.com/post/TxFKSK2QJE6RPZ/Upcoming-Stable-Release-of-AWS-SDK-for-Ruby-Version-2

I determined the issue. I recreated the Buckets. You have to have the Region set to "US Standard" for it to work. Originally it was set to "Oregon."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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