Skip to content

Instantly share code, notes, and snippets.

@keenahn
Created August 16, 2012 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keenahn/3374574 to your computer and use it in GitHub Desktop.
Save keenahn/3374574 to your computer and use it in GitHub Desktop.
Ruby: Nicedit + Carrierwave for image uploads
class Image < ActiveRecord::Base
attr_accessible :f
mount_uploader :f, ImageUploader
end
# Assumes that your field name in HTML is "image" and that the data is posted to a URL that triggers the create method below
class ImageController < ApplicationController
skip_before_filter :verify_authenticity_token # This is a hack, but need it for now. TODO: add the CSRF token to the nicedit request
def create
a = Image.new
a.send("f=", params[:image]) # This tripped me up! Can't directly assign a.f = params[:image]
a.save!
if a.errors.any?
# TODO SOMETHING
else
# This also tripped me up, getting the exact right format that nicedit was expecting.
# assumes that in your ImageUploader carrierwave class you have a version called "medium"
render json: {upload: {
links:{original: "#{a.f.medium.url}"}
}}
end
end
end # close ImageController
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment