html - How do I save images via options_for_select with Paperclip and Image Picker on Rails 4? -
i'm trying save images via form populates "select" field number of options. select form uses image picker change options when user clicks on selected image. when form submitted, selected image should saved paperclip gem.
i think need point image url selected option method open in paperclip, i'm not quite sure how that. i'm new rails, , humbly appreciate point me in right direction!
here's 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 use ":medium", when setting form input, error says "activerecord::associationtypemismatch @ /stories medium(#70334728193840) expected, got string(#70334669736760)", have tried using ":medium_id", displays value of selected option instead of url.
using ":medium_id" allows rest of form fields save without error, option value saved image, not 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 |format| if @medium.save format.html { redirect_to @medium, notice: 'medium 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 |t| t.integer "medium_id" end create_table "media", force: true |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 following without success:
save image url paperclip which proper way upload image external url 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.
Comments
Post a Comment