Ruby on rails Rails回形针图像URL上载-不保存图像
对rails来说还是很新的。我有一个书评应用程序,通过回形针上传图片。如果您通过文件字段上传图像,它可以正常工作,但是我想添加通过链接上传图像的功能。到目前为止,我失败得很惨,所以我向你们求助。导轨和回形针都是最新版本 提交表单时,控制台中会显示以下内容:Ruby on rails Rails回形针图像URL上载-不保存图像,ruby-on-rails,ruby,paperclip,Ruby On Rails,Ruby,Paperclip,对rails来说还是很新的。我有一个书评应用程序,通过回形针上传图片。如果您通过文件字段上传图像,它可以正常工作,但是我想添加通过链接上传图像的功能。到目前为止,我失败得很惨,所以我向你们求助。导轨和回形针都是最新版本 提交表单时,控制台中会显示以下内容: Started POST "/books" for ::1 at 2015-12-24 20:49:55 -0600 Processing by BooksController#create as HTML Parameters: {"u
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,但没有保存它。
它显示为缺少的.png
在控制台中,如果我选中Book.last,它会显示图像\u 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_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
我肯定我可能错过了这么简单的东西。当我打开了似乎与这个问题相关的所有链接,不知道还有什么可以尝试时,它会变得更加混乱。任何帮助都将不胜感激
如果有什么我忘了包括的细节,请告诉我。谢谢更换
与
将表单中的图书url更改为图书img或图像url(-:不,OP确实有图像url迁移。img\u url没有显示在表单提交中的原因不是因为它不在数据库中,无论如何。@qubit我显然是新来的,所以我需要更多的解释。对不起,谢谢你的帮助,虽然我在OP中添加了部分表单。谢谢你的帮助helping@tdog检查我的答案hings Occessed:现在,当您添加一本书时,该字段会自动填充到缺少的png的链接。我如何避免这种情况?其次,当我提交表单时,它会给出一个错误undefined method image='for
,并指向图书模型self.image=URI.parse(url\u值)中的这一行
不过,我觉得我们走的方向是对的!我不清楚你在这里想做什么。你能澄清一下,上传带有链接的图像是什么意思吗?我认为,在post参数中,与图像相关的唯一一件事就是你试图上传的内容?是的,没错。我想通过链接上传图像,在这种情况下,我想选择一个链接mazon…而不是用户需要在他们的机器上上传文件。我不认为这是不寻常的?所以你想下载该图像并存储在你的本地机器和/或你自己的云上?在这种情况下,看看这个事实,我想这可能是一个问题,因为它自己的线程,我不知道如何保存图像s、 我知道现在它正在把它们保存到我机器上的一个临时文件夹中,但还没有在生产中处理这个宝石。感谢链接,是时候阅读了。在制作这个之前,我已经阅读了这个线程,也许我的执行已经停止,但这些解决方案仍然不适用于我。继续显示缺失的。pngunnith呃,他们中的一个成功了:(不过谢谢你的帮助
def book_params
params.require(:book).permit(:title, :description, :author, :category_id, :book_img, :image_url)
end
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 %>
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