Ruby on Rails Friday, January 16, 2015

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:
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/.

--
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-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1e76dd91-d335-414b-afb8-ced8cac200e6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment