Duplicate record including paperclip image

138 views
Skip to first unread message

Tony T.

unread,
Jan 8, 2015, 1:41:39 PM1/8/15
to rubyonra...@googlegroups.com
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


--
Posted via http://www.ruby-forum.com/.

Colin Law

unread,
Jan 8, 2015, 4:08:17 PM1/8/15
to rubyonra...@googlegroups.com
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

Tony T.

unread,
Jan 8, 2015, 5:47:33 PM1/8/15
to rubyonra...@googlegroups.com
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?

Vivek Sampara

unread,
Jan 9, 2015, 1:43:01 AM1/9/15
to rubyonra...@googlegroups.com
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. 

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/6e95b8053d293ce22a54c412ee4eb79c%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

Colin Law

unread,
Jan 9, 2015, 4:35:44 AM1/9/15
to rubyonra...@googlegroups.com
On 8 January 2015 at 22:46, Tony T. <li...@ruby-forum.com> wrote:
> 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

Tony T.

unread,
Jan 9, 2015, 1:49:04 PM1/9/15
to rubyonra...@googlegroups.com
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:
https://github.com/thoughtbot/paperclip 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:
http://stackoverflow.com/questions/27810317/duplicate-record-with-only-certain-attributes-in-rails-4/27811021#27811021

Thanks again for any help!

Tony Tambe

unread,
Jan 15, 2015, 8:49:02 PM1/15/15
to rubyonra...@googlegroups.com

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.

Tony Tambe

unread,
Jan 16, 2015, 1:40:05 PM1/16/15
to rubyonra...@googlegroups.com
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:

    <div class="form-group">    
      <%= f.input :image_remote_url, label: "or enter a URL" %>
    </div>

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.

On Thursday, January 8, 2015 at 1:41:39 PM UTC-5, Ruby-Forum.com User wrote:
Reply all
Reply to author
Forward
0 new messages