在 Ruby 中添加图片和资源后无法保存图书
I can't save a book after adding image and resource in Ruby
我需要这方面的帮助。我正在使用 ruby 和亚马逊网络服务创建一个演示书店。我已经安装了 gem 'aws-sdk' 并且一切正常,直到我尝试使用图像和资源创建新书。我收到 link 中列出的错误。 "app/controllers/books_controller.rb:29:in `create' "(抱歉,我添加了 Full Trace,因为我认为这会有所帮助)如果我删除它建议的代码行,它会起作用,但该书不会显示在仪表板中。
如果您需要我的帮助,请告诉我。我没有将最新的提交推送到 github 因为我不想出现任何错误,但如果需要的话,我会的。 Here 是没有这些功能的最新回购协议。
这是我为达到这一点所采取的步骤。也许我错过了什么?
将 aws-sdk gem 添加到我的 gem 文件
设置回形针默认为 AWS S3 并设置其凭据
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']
}
}
将两个附件(:图像/:资源)输入字段添加到书籍表单中
<%= 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 %>
验证附件的内容类型及其存在
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
然后当我尝试创建一本新书时出现错误"books/new"
更新:
这是我点击 "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)
这是由新的 aws-sdk gem 引起的,请更改您的 gem 文件以安装比 2.0
更旧的版本
gem 'aws-sdk', '< 2.0'
它应该可以工作。
更多信息http://ruby.awsblog.com/post/TxFKSK2QJE6RPZ/Upcoming-Stable-Release-of-AWS-SDK-for-Ruby-Version-2
我确定了这个问题。我重新创建了桶。您必须将区域设置为 "US Standard" 才能正常工作。最初它被设置为 "Oregon."
我需要这方面的帮助。我正在使用 ruby 和亚马逊网络服务创建一个演示书店。我已经安装了 gem 'aws-sdk' 并且一切正常,直到我尝试使用图像和资源创建新书。我收到 link 中列出的错误。 "app/controllers/books_controller.rb:29:in `create' "(抱歉,我添加了 Full Trace,因为我认为这会有所帮助)如果我删除它建议的代码行,它会起作用,但该书不会显示在仪表板中。
如果您需要我的帮助,请告诉我。我没有将最新的提交推送到 github 因为我不想出现任何错误,但如果需要的话,我会的。 Here 是没有这些功能的最新回购协议。
这是我为达到这一点所采取的步骤。也许我错过了什么? 将 aws-sdk gem 添加到我的 gem 文件
设置回形针默认为 AWS S3 并设置其凭据
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']
}
}
将两个附件(:图像/:资源)输入字段添加到书籍表单中
<%= 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 %>
验证附件的内容类型及其存在
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
然后当我尝试创建一本新书时出现错误"books/new"
更新:
这是我点击 "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)
这是由新的 aws-sdk gem 引起的,请更改您的 gem 文件以安装比 2.0
更旧的版本gem 'aws-sdk', '< 2.0'
它应该可以工作。
更多信息http://ruby.awsblog.com/post/TxFKSK2QJE6RPZ/Upcoming-Stable-Release-of-AWS-SDK-for-Ruby-Version-2
我确定了这个问题。我重新创建了桶。您必须将区域设置为 "US Standard" 才能正常工作。最初它被设置为 "Oregon."