在Ruby中添加图像和资源后,我无法保存图书
问题描述:
我需要帮助。我正在创建一个使用ruby和amazon web服务的示例书店。我已经安装了gem'aws-sdk',一切正常,直到我尝试用图像和资源创建新书。我收到link上列出的错误。 “app/controllers/books_controller.rb:29:在'create'”(对不起,我添加了完整跟踪,因为我认为这会有所帮助)如果我删除它建议的代码行,它可以工作,但书不会在仪表板中显示。在Ruby中添加图像和资源后,我无法保存图书
让我知道你是否需要我的任何东西。我没有把最新的提交推送到github上,因为我不想要任何错误,但是如果需要的话,我会的。 Here是没有这些功能的最新回购。
下面是我采取的步骤来达到这一点。也许我错过了什么? 添加AWS-SDK宝石我的Gemfile
设置回形针默认为AWS S3并设置其凭据
应用程序/配置/ 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
然后是错误,当我尝试创建一个新的书“的书籍/新”
UPDATE:
这里是我的终端出认沽当我点击“创建书”
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引起的,请将您的gemfile更改为安装旧版本2.0以上版本
gem 'aws-sdk', '< 2.0'
它应该工作。
欲了解更多信息http://ruby.awsblog.com/post/TxFKSK2QJE6RPZ/Upcoming-Stable-Release-of-AWS-SDK-for-Ruby-Version-2
我试过了,仍然没有运气。仍然收到错误! – jshuadvd 2015-03-03 05:08:11