Duplicate record including paperclip image

My site is for posting album reviews, which are called Pins. The pins model has the following attributes:

:artist, :year, :title, :rank, :description, and :image

The image uses Paperclip and is stored on Amazon S3 if that matters

I am trying to allow a user to see a review that another user posted and click a link to more simply write their own review for that same album. So basically the link takes them to the Pin.new page and the form already has the :artist, :title, :image, and :year filled in.

I figured out how to do it while bringing all of the attributes that I want, but the image doesn't come over to the new form.

Here is the pins_controller.rb code I'm using, which gives me an error of "no implicit conversion of URI::Generic into String":

  def copy     @source = Pin.find(params[:id])     @image = URI.parse(@source.image.url)     @pin = Pin.new(artist: @source.artist, album: @source.album, year: @source.year, image: @image)     render 'new'   end

And in my show view:

<%= link_to "copy", copy_pin_path(params[:id]) %>

Attachments: http://www.ruby-forum.com/attachment/10385/Screen_Shot_2015-01-08_at_1.39.06_PM.png

For future reference it is better to copy the error text from the terminal window and paste it here rather than attaching an image, then I would be able to insert my text at the appropriate point in the error.

I assume that the image field in the db is a string field. You are calling Pin.new with a URI object as the image and the error says that there is not an implicit way of converting the URI to a string. I guess that you want to use @image.to_s in Pin.new.

Colin

Colin Law wrote in post #1166334:

For future reference it is better to copy the error text from the terminal window and paste it here rather than attaching an image, then I would be able to insert my text at the appropriate point in the error.

I assume that the image field in the db is a string field. You are calling Pin.new with a URI object as the image and the error says that there is not an implicit way of converting the URI to a string. I guess that you want to use @image.to_s in Pin.new.

Colin

Hi Colin,   Thanks for answering. I posted "Here is the pins_controller.rb code I'm using, which gives me an error of "no implicit conversion of URI::Generic into String"" in the question above. Is there something else that I should have added so that I know in the future?

I thought of adding .to_s to @image, but that leads me to the error: Paperclip::adapterregistry::nohandlererror in PinsController#copy No handler found for "/system/pins/images/000/000/083/original/Alt-J_-_This_is_all_yours.jpg?1420125230"

I think I need to tell it to just pic out the .jpg itself, not the whole directory listing. Does that sound right?

Hi,

Its because of the image column in the database is “string” probably and @image returns an object of “URI::Generic” . Can you show the schema of pin model and the gem you’re using for attachments handling ?

meanwhile try , @image.to_s on that line.

Colin Law wrote in post #1166334:

For future reference it is better to copy the error text from the terminal window and paste it here rather than attaching an image, then I would be able to insert my text at the appropriate point in the error.

I assume that the image field in the db is a string field. You are calling Pin.new with a URI object as the image and the error says that there is not an implicit way of converting the URI to a string. I guess that you want to use @image.to_s in Pin.new.

Colin

Hi Colin,   Thanks for answering. I posted "Here is the pins_controller.rb code I'm using, which gives me an error of "no implicit conversion of URI::Generic into String"" in the question above. Is there something else that I should have added so that I know in the future?

Yes, as I said, you attached an image instead of copy/pasting the equivalent text out of the terminal window (including some of the stack trace if appropriate). When asking questions it is important to make life as easy as possible for those who may be able to help, to understand the question. We are used to seeing error messages in the terminal window and immediately understanding the context. In order to see which line was generating the error I had to go and look at the image.

I thought of adding .to_s to @image, but that leads me to the error: Paperclip::adapterregistry::nohandlererror in PinsController#copy No handler found for "/system/pins/images/000/000/083/original/Alt-J_-_This_is_all_yours.jpg?1420125230"

Because, again, you have not copy/pasted the complete message, and possibly stack trace, from the terminal I do not know which line of code generated that error so have no idea of the cause.

I think I need to tell it to just pic out the .jpg itself, not the whole directory listing. Does that sound right?

<shrug/>

Colin

Colin Law wrote in post #1166375:

I thought of adding .to_s to @image, but that leads me to the error: Paperclip::adapterregistry::nohandlererror in PinsController#copy No handler found for

"/system/pins/images/000/000/083/original/Alt-J_-_This_is_all_yours.jpg?1420125230"

Because, again, you have not copy/pasted the complete message, and possibly stack trace, from the terminal I do not know which line of code generated that error so have no idea of the cause.

I think I need to tell it to just pic out the .jpg itself, not the whole directory listing. Does that sound right?

<shrug/>

Colin

Hi Colin, and thanks again for taking the time. Understood that you want the terminal error. This is the full error that I get in the terminal tail:

Paperclip::AdapterRegistry::NoHandlerError (No handler found for "/system/pins/images/000/000/084/original/Angel.png?1420769160"):   app/controllers/pins_controller.rb:46:in `copy'

I read through the Paperclip gem documentation on Github: GitHub - thoughtbot/paperclip: Easy file attachment management for ActiveRecord but that hasn't gotten me anywhere.

I;m not sure if this helps as well, but here is what an image looks like when I call it in the console. First with adding .to_s to see it as a string, and secondly to show it without converting it to string.

2.0.0-p353 :019 > p.image.to_s => "/system/pins/images/000/000/083/original/Alt-J_-_This_is_all_yours.jpg?1420125230" 2.0.0-p353 :020 > p.image => #<Paperclip::Attachment:0x0000010611a3f8 @name=:image, @instance=#<Pin id: 83, description: "<p>Good</p>\r\n", created_at: "2015-01-01 15:13:51", updated_at: "2015-01-01 15:13:51", user_id: 3, image_file_name: "Alt-J_-_This_is_all_yours.jpg", image_content_type: "image/jpeg", image_file_size: 23265, image_updated_at: "2015-01-01 15:13:50", image_remote_url: nil, artist: "Alt-J", album: "This Is All Yours", date: nil, rank: nil, video_html: "", video: "", rating: "3", year: "2014", title: "">, @options={:convert_options=>{}, :default_style=>:original, :default_url=>"/:attachment/:style/missing.png", :escape_url=>true, :restricted_characters=>/[&$+,\/:;=?@<>\[\]\{\}\|\\\^~%# ]/, :filename_cleaner=>nil, :hash_data=>":class/:attachment/:id/:style/:updated_at", :hash_digest=>"SHA1", :interpolator=>Paperclip::Interpolations, :only_process=>, :path=>":rails_root/public:url", :preserve_files=>false, :processors=>[:thumbnail], :source_file_options=>{}, :storage=>:filesystem, :styles=>{:medium=>"320x240>"}, :url=>"/system/:class/:attachment/:id_partition/:style/:filename", :url_generator=>Paperclip::UrlGenerator, :use_default_time_zone=>true, :use_timestamp=>true, :whiny=>true, :check_validity_before_processing=>true}, @post_processing=true, @queued_for_delete=, @queued_for_write={}, @errors={}, @dirty=false, @interpolator=Paperclip::Interpolations, @url_generator=#<Paperclip::UrlGenerator:0x0000010611a290 @attachment=#<Paperclip::Attachment:0x0000010611a3f8 ...>, @attachment_options={:convert_options=>{}, :default_style=>:original, :default_url=>"/:attachment/:style/missing.png", :escape_url=>true, :restricted_characters=>/[&$+,\/:;=?@<>\[\]\{\}\|\\\^~%# ]/, :filename_cleaner=>nil, :hash_data=>":class/:attachment/:id/:style/:updated_at", :hash_digest=>"SHA1", :interpolator=>Paperclip::Interpolations, :only_process=>, :path=>":rails_root/public:url", :preserve_files=>false, :processors=>[:thumbnail], :source_file_options=>{}, :storage=>:filesystem, :styles=>{:medium=>"320x240>"}, :url=>"/system/:class/:attachment/:id_partition/:style/:filename", :url_generator=>Paperclip::UrlGenerator, :use_default_time_zone=>true, :use_timestamp=>true, :whiny=>true, :check_validity_before_processing=>true}>, @source_file_options={}, @whiny=true, @normalized_styles={:medium=>#<Paperclip::Style:0x000001061211d0 @name=:medium, @attachment=#<Paperclip::Attachment:0x0000010611a3f8 ...>, @geometry="320x240>", @format=nil, @other_args={}>}>

I have a question on Stack Overflow as well:

Thanks again for any help!

I’ve been working on this more and in the console I can copy from one Pin record and save a new pin like this:

r = Pin.new
r.image = Pin.find(83).image
r.save

I think the issue is that the new pin form requires that an image is chosen from the user’s PC before saving the pin. Whatever that function of Paperclip is, I think that is where I’m getting stuck.

I figured out a work around and I figured I’d post it here in case anyone can benefit.

You can set up Paperclip to load an image using a url instead of selecting a file from your local PC. I had this set up, but haven’t used it in a while. (I had removed that form field from the new form, so that it couldn’t be accessed) You can find tutorials online to set that up. To make my app work, I added the field back to the form:

<%= f.input :image_remote_url, label: “or enter a URL” %>

And my controller now is:

def copy

@source = Pin.find(params[:id])

@image = @source.image.url

@pin = Pin.new(artist: @source.artist, album: @source.album, year: @source.year, image_remote_url: @image)

render ‘new’

end

And I had to add .to_s to the image_remote_url method in my model:

def image_remote_url=(url_value)

self.image = URI.parse(url_value).to_s unless url_value.blank?

super

end

Now when a user clicks a button on a record in my app, which is coded:

<%= link_to “copy”, copy_pin_path(params[:id]) %>

It takes them to the new form, with the artist, album, year, and the url of the image already filled in.