簡體   English   中英

Rails回形針圖像URL上傳 - 不保存圖像

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

對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)

它似乎是承認圖像網址,但不保存它。 它看起來像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> 

很明顯我設置錯了。 我已經按照我在這里找到的所有鏈接進行了跟蹤,並嘗試將它們與我最初的工作方式結合起來。

這是我的書模型:

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

我敢肯定我可能錯過了這么簡單的事情。 當我打開似乎與此問題相關的每個鏈接並且不知道還有什么可以嘗試時,它會變得更加混亂。 任何幫助將非常感激!

如果有任何我忘記包含的細節,請告訴我。 謝謝

更換
<%= text_field_tag 'image_url' %>

<%= f.input 'image_url' %>

在你改變book_url形成以book_img圖片網址 ( - :

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM