Created
August 16, 2012 23:45
-
-
Save keenahn/3374574 to your computer and use it in GitHub Desktop.
Ruby: Nicedit + Carrierwave for image uploads
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Image < ActiveRecord::Base | |
attr_accessible :f | |
mount_uploader :f, ImageUploader | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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