1

I'm trying to save images via a form that populates a "Select" field with a number of options. The select form uses Image Picker to change the options when an user clicks on a selected image. When the form is submitted, the selected image should be saved with the Paperclip gem.

I think I just need to point the image url from the selected option to a method that will open it in Paperclip, but I'm not quite sure how to do that. I'm new to Rails, and humbly appreciate anything that will point me in the right direction!

Here's what I have:

_form.html.erb

<%= f.select( :medium_id,
              options_for_select(@story.fetched_images.collect{ |v,i| [v, @story.fetched_images.index(v), 'data-img-src' => v ] } )) %>

If I use ":medium", as I would normally do when setting the form input, I get an error that says "ActiveRecord::AssociationTypeMismatch at /stories Medium(#70334728193840) expected, got String(#70334669736760)", so I have tried using ":medium_id", which displays the value of the selected option instead of the url.

Using ":medium_id" allows the rest of the form fields to save properly without an error, but only the option value is saved for the image, not the image itself.

story.rb

class Story < ActiveRecord::Base

belongs_to :medium

accepts_nested_attributes_for :medium

stories_controller.rb

class StoriesController < ApplicationController

def new
    @title = "Submit Story"
    @cur_url = "/stories/new"


    @story = Story.new
    @story.build_medium
end

private

def story_params
    p = params.require(:story).permit(
  :title, :url, :description, :moderation_reason, :seen_previous,
  :merge_story_short_id, :is_unavailable, :medium, :medium_id, :tags_a => [], medium_attributes: [:medium, :image],
)

medium.rb

require "open-uri"

class Medium < ActiveRecord::Base

has_one :story

before_save :image_from_select

    def image_from_select(url)
        self.image = open(url)
    end

end

media_controller.rb

def create
    @medium = Medium.new(medium_params)

    respond_to do |format|
      if @medium.save
        format.html { redirect_to @medium, notice: 'Medium was successfully created.' }
        format.json { render action: 'show', status: :created, location: @medium }
      else
        format.html { render action: 'new' }
        format.json { render json: @medium.errors, status: :unprocessable_entity }
      end
    end
  end

private

def set_medium
  @medium = Medium.find(params[:id])
end


def medium_params
  params.require[:medium].permit(:image)
end

end

schema.rb

create_table "stories", force: true do |t|
  t.integer  "medium_id"
end

  create_table "media", force: true do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
    t.integer  "story_id"
  end

I have tried referencing the following without success:

Save image from URL by paperclip Which is the proper way to upload an image from an external url with Paperclip on rails 4? http://runnable.com/UnsMH7fYN2V6AAAT/how-to-save-images-by-url-with-paperclip-for-ruby-on-rails-and-upload https://github.com/thoughtbot/paperclip/wiki/Attachment-downloaded-from-a-URL

Thanks in advance.

Community
  • 1
  • 1
alphaleonis
  • 1,329
  • 11
  • 18

0 Answers0