【发布时间】:2016-03-31 07:24:21
【问题描述】:
对于 Rails 来说还是很新的。我有一个书评应用程序,通过回形针上传图片。如果您通过文件字段上传图像,它工作正常,但我想添加通过链接上传图像的功能。到目前为止,我一直很失败,所以我转向你们。导轨和回形针都是最新版本。
当我提交表单时,控制台中会显示以下内容:
Started POST "/books" for ::1 at 2015-12-24 20:49:55 -0600
Processing by BooksController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"UGX2ed+eVP9nuLoUEo/pDNX6CpICbOZgIv6U3OPcxmbRtmmIesRNki1Zv/7rQcOlqQEpsAuZVx9NjCe9mdizcQ==", "category_id"=>"", "book_url"=>"http://ecx.images-amazon.com/images/I/41aQPTCmeVL.jpg", "book"=>{"title"=>"The Hobbit", "description"=>"A wonderful journey", "author"=>"J.R.R Tolkien"}, "commit"=>"Create Book"}
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
(0.4ms) begin transaction
SQL (0.9ms) INSERT INTO "books" ("title", "description", "author", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["title", "The Hobbit"], ["description", "A wonderful journey"], ["author", "J.R.R Tolkien"], ["user_id", 1], ["created_at", "2015-12-25 02:49:55.555658"], ["updated_at", "2015-12-25 02:49:55.555658"]]
(1.4ms) commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 50ms (ActiveRecord: 3.6ms)
它似乎正在确认图像 url,但没有保存它。 它显示为 missing.png。
如果我在控制台中检查 Book.last,它会显示 image_url 为 nil:
SELECT "books".* FROM "books" ORDER BY "books"."id" DESC LIMIT 1
=> #<Book id: 25, title: "The Hobbit", description: "kJSdfkjsdkfjsjf", author: "J.R.R Tolkien", created_at: "2015-12-25 02:37:10", updated_at: "2015-12-25 02:37:10", user_id: 1, category_id: 1, book_img_file_name: nil, book_img_content_type: nil, book_img_file_size: nil, book_img_updated_at: nil, image_remote_url: nil, image_url_file_name: nil, image_url_content_type: nil, image_url_file_size: nil, image_url_updated_at: nil>
很明显我的设置有误。我已经关注了我能找到的所有链接,并尝试将它们与我最初的工作方式结合起来。
这是我的 Book 模型:
class Book < ActiveRecord::Base
belongs_to :user
belongs_to :category
has_many :reviews
attr_reader :image_url
has_attached_file :book_img, :styles => { :book_index => "250x350>", :book_show => "325x475>" }
has_attached_file :image_url, :styles => { :book_index => "250x350>", :book_show => "325x475>" }, :url => "/:class/:attachment/:id/:style_:basename.:extension"
validates_attachment_content_type :book_img, :content_type => /\Aimage\/.*\Z/
def image_url=(url_value)
self.image = URI.parse(url_value)
@image_url = url_value
end
end
这是来自书籍控制器的书籍参数:
def book_params
params.require(:book).permit(:title, :description, :author, :category_id, :book_img, :image_url)
end
这里是从 book_img 实际有效的迁移,然后是对 image_url 无效的迁移。
class AddAttachmentBookImgToBooks < ActiveRecord::Migration
def self.up
change_table :books do |t|
t.attachment :book_img
end
end
def self.down
remove_attachment :books, :book_img
end
end
class AddAttachmentImageUrlToBooks < ActiveRecord::Migration
def self.up
change_table :books do |t|
t.attachment :image_url
end
end
def self.down
remove_attachment :books, :image_url
end
end
这是部分表单
<%= simple_form_for @book, :html => { :multipart => true } do |f| %>
<% if @book.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@book.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% @book.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= select_tag(:category_id, options_for_select(@categories), :prompt => "Select a Category") %>
<%= f.file_field :book_img %>
<%= text_field_tag 'image_url' %>
<%= f.input :title, label: "Book Title" %>
<%= f.input :description %>
<%= f.input :author %>
<%= f.button :submit, :class => 'btn-custom2' %>
<% end %>
以及Schema.rb文件的相关部分
create_table "books", force: :cascade do |t|
t.string "title"
t.text "description"
t.string "author"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.integer "category_id"
t.string "book_img_file_name"
t.string "book_img_content_type"
t.integer "book_img_file_size"
t.datetime "book_img_updated_at"
t.string "image_remote_url"
t.string "image_url_file_name"
t.string "image_url_content_type"
t.integer "image_url_file_size"
t.datetime "image_url_updated_at"
end
我确定我可能遗漏了一些如此简单的东西。当我打开似乎与此问题相关的每个链接并且不知道还能尝试什么时,它只会变得更加混乱。任何帮助将不胜感激!
如果有任何我忘记包含的细节,请告诉我。谢谢
【问题讨论】:
-
我不清楚你想在这里做什么。你能澄清一下上传带有链接的图片是什么意思吗?我认为与帖子参数中的图片相关的唯一内容是 ecx.images-amazon.com/images/I/41aQPTCmeVL.jpg...is 您要上传的内容?
-
是的,完全正确。在这种情况下,我希望选择通过指向亚马逊的链接上传图像......而不是用户需要在他们的机器上拥有文件来上传它。我不认为这很罕见吗?
-
所以您想下载该图像并将其存储在您的本地计算机和/或您自己的云中?在这种情况下,看看这个stackoverflow.com/questions/4049709/…
-
其实我猜这可能是它自己的线程的问题,我不知道保存图像。我现在知道它正在将它们保存到我机器上的临时文件夹中,但还没有在生产中处理这个 gem。感谢您的链接,有时间阅读一下
-
好吧,在制作这个之前,我已经通过了那个线程,也许我的执行已经关闭,但这些解决方案仍然不适合我。继续显示missing.png
标签: ruby-on-rails ruby paperclip