简体   繁体   English

Rails回形针图像URL上传 - 不保存图像

[英]Rails paperclip image URL uploading - not saving image

Still pretty new to rails. 对rails来说还是个新手。 I have a book reviews app, with image uploading via paperclip. 我有一个书评应用程序,通过回形针上传图像。 It works fine if you upload an image through the file field, but I want to add the ability to upload images via a link. 如果您通过文件字段上传图像,它可以正常工作,但我想添加通过链接上传图像的功能。 So far, I have failed hard, so I'm turning to you guys. 到目前为止,我已经失败了,所以我转向你们。 Both rails and paperclip are the latest versions. 导轨和回形针都是最新版本。

When I submit the form, this is what shows in the console: 当我提交表单时,这是控制台中显示的内容:

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)

It seems that it is acknowledging the image url, but not saving it. 它似乎是承认图像网址,但不保存它。 It appears as a missing.png instead. 它看起来像missing.png。

In the console if I check Book.last, it shows the image_url is nil: 在控制台中,如果我检查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> 

So clearly I have something setup wrong. 很明显我设置错了。 I have followed all the links on here I could find, and tried to combine them with how I got it working originally. 我已经按照我在这里找到的所有链接进行了跟踪,并尝试将它们与我最初的工作方式结合起来。

Here's my Book model: 这是我的书模型:

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

Here are the book params from the books controller: 以下是书籍控制器中的书籍参数:

def book_params
      params.require(:book).permit(:title, :description, :author, :category_id, :book_img, :image_url)
end

Here's the migration from book_img that actually works, followed by the migration for image_url that does not. 这是从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

Here's the form partial 这是部分形式

<%= 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 %>

And the relevant portion of the Schema.rb file 以及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

I'm sure I'm probably missing something so simple. 我敢肯定我可能错过了这么简单的事情。 It just gets extra confusing when I've opened what seems to be every link related to this issue and don't know what else to try. 当我打开似乎与此问题相关的每个链接并且不知道还有什么可以尝试时,它会变得更加混乱。 Any help would be so greatly appreciated! 任何帮助将非常感激!

Let me know if there are any details I forgot to include. 如果有任何我忘记包含的细节,请告诉我。 Thanks 谢谢

replace 更换
<%= text_field_tag 'image_url' %>
with
<%= f.input 'image_url' %>

在你改变book_url形成以book_img图片网址 ( - :

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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