1

Still pretty new to 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.

In the console if I check Book.last, it shows the image_url is 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.

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

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

tdog
  • 147
  • 2
  • 10
  • I am not clear what are you trying to do here. Can you clarify what do you mean by upload an image with a link? The only thing I think that is related to image in the post params is http://ecx.images-amazon.com/images/I/41aQPTCmeVL.jpg...is that what you are trying to upload? – qubit Dec 25 '15 at 08:15
  • Yes, exactly. I want the option to upload an image via a link to in this case amazon... instead of the user needing to have the file on their machine to upload it. I didn't think that was that uncommon? – tdog Dec 26 '15 at 02:18
  • so you want to download that image and store in your local machine and/or your own cloud? In that case, take a look at this http://stackoverflow.com/questions/4049709/save-image-from-url-by-paperclip – qubit Dec 26 '15 at 03:57
  • Actually I guess that's probably a question for it's own thread, I have no idea about saving the images. I know right now it's saving them to a temp folder on my machine, but haven't dealt with this gem in production. Thanks for the link, time for some reading – tdog Dec 26 '15 at 17:51
  • Okay I had already gone through that thread before making this one, maybe my execution was off but these solutions still aren't working for me. Keep showing the missing.png – tdog Dec 26 '15 at 18:15

2 Answers2

2

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

seoyoochan
  • 822
  • 3
  • 14
  • 28
  • No, OP does have the image_url migration. And the reason the img_url is not shown in the form submit is not because it is not in the database, in any case. – qubit Dec 25 '15 at 08:11
  • @qubit I'm obviously new so I'll need a little more explanation than that. Sorry, thanks for the help though – tdog Dec 26 '15 at 02:19
  • I added the form partial to the OP. Thanks for helping – tdog Dec 26 '15 at 02:51
  • @tdog check my answer. – seoyoochan Dec 26 '15 at 05:20
  • 2 things happened: Now when you go to add a book, that field is automatically filled with the link to the missing png. How do I avoid that? Second, when I submit the form, it gives an error `undefined method image=' for #` and points to this line in the books model `self.image = URI.parse(url_value)` I feel like we're on the right track though! – tdog Dec 26 '15 at 17:56
  • I don't get what you mean by the field is automatically filled. Did you refresh the page before trying to add a book? That second error message means that the book model does not have a instance method of `image`. this is because your `self.image` means a class method. replace it with `image` @tdog – seoyoochan Dec 26 '15 at 18:41
  • change self.image to self.image_url – qubit Dec 27 '15 at 02:07
  • @seoyoochan, self.image in an instance method, NOT a class method. It simply means there is no method defined on the object #. tdog, there is no image on the book object, there is only image_url and book_img (not sure why you have two anyone, I would have one only). – qubit Dec 27 '15 at 02:10
  • @qubit sorry it is definitely a class method. i was confused with other stuff – seoyoochan Dec 27 '15 at 03:51
  • @seoyoochan When I go to the Add Book page, the field where you enter the link is already filled with `/image_urls/original/missing.png`. – tdog Dec 27 '15 at 19:17
  • Well @qubit obviously I'd like to have one instead of two, how do I do that? I had it set up originally without the url, and decided to add that feature later. This is the 2nd thing I've made in rails, so cut me some slack... I'm here asking for help from people who DO know how to implement it correctly – tdog Dec 27 '15 at 19:21
  • Also after changing `self.image` to `self.image_url` I'm getting this: `bad URI(is not URI?): /image_urls/original/missing.png` – tdog Dec 27 '15 at 19:22
  • @tdog, so what I understand how this works is this: Paperclip store all image via has_attachement: book_image , however, the source of the image can be both from a remote link or user file upload from their local machine. Paperclip will handle this for you. And yes, /image_urls/origina/missing.png is not an url. Make sure your form submits http://ecx.images-amazon.com/images/I/41aQPTCmeVL.jpg,which starts with http:// or https://. You can test in your rails console , – qubit Dec 27 '15 at 23:41
  • , rails c > self.book_image = URI.parse('http://ecx.images-amazon.com/images/I/41aQPTCmeVL.jpg'). However I believe you should be using URI.open instead of URI.parse. Based on the other SO question I pasted earlier. – qubit Dec 27 '15 at 23:41
  • After switching to URI.open, I'm getting `private method 'open' called for URI:Module` and with URI.parse it's still telling me invalid url. Thanks for your help and patience – tdog Dec 28 '15 at 02:10
  • Also when I try that code above in the console, I get `undefined method book_img` – tdog Dec 28 '15 at 02:13
1

Change book_url in you form to book_img or image_url (-:

Vetal4eg
  • 182
  • 1
  • 6
  • 17