1

I'm currently building a very simple API in rails. All I need to do is upload text and images on heroku, store that uploaded data on S3 in JSON (so I'm guessing rendering it as JSON when saving?) and pull that data into my frontend via AJAX. I'm using the paperclip and 'aws-sdk' gem and I'm succesfully saving images into my S3 bucket, I just need to serialize it into JSON.

In my controller I have:

def create
    @project = Project.new(project_params)
    render :json => @project

    @project.save
    redirect_to @project
end

My model:

class Project < ActiveRecord::Base

    has_attached_file :photo, :styles => { :small => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
    validates_attachment_content_type :photo, :content_type => /\Aimage\/.*\Z/

end

and In my production environment I have:

config.paperclip_defaults = {
    :storage => :s3,
    :s3_credentials => {
    :bucket => ENV['S3_BUCKET_NAME'],
    :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
    :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
    }
  }

At present this is just saving the images into subfolders in my bucket, I would like to save this into one folder and render it is as JSON. I'm new to rails so apologies if I have missed anything out. Thanks

biscuit_
  • 187
  • 1
  • 3
  • 16
  • There are some decisions you need to make if you want to serialize image data to JSON, not related to rails but to the fact that image data is binary, so you need to decide on an encoding (i.e. something like Base64), and on how exactly to serialize to JSON as there is no standard way that I know of. – taglia Jan 11 '16 at 01:38
  • @taglia Sorry, I wasn't clear. What I mean is the image itself does not need to converted, but I need to store the URL along with the text and title into JSON. In my params I have tried setting this with the likes of params.require(:project).permit(:title, :text, :photo_url) however I have had no success – biscuit_ Jan 11 '16 at 10:38

1 Answers1

0

As I understand, you already got paperclip to work correctly, so you actually already have the required info (they are saved by paperclip), you just need to render them to JSON. You can ignore the folder structure created by paperclip, the gem manages it internally and generates the URLs correctly.

If you need the a JSON response to respond API calls from a controller action, you just need to do this:

render json: { name: @project.photo_file_name, url: @project.photo.url }

Of course you can enrich your JSON with any parameters you need.

This is an over-simplified answer, to do this "the rails way" you should have a look at JBuilder, and create JSON view templates, the same way you do for HTML templates. Or override .as_json(options = nil) in your model, so it will automatically generate the JSON format you need when you serialize it.

taglia
  • 1,837
  • 2
  • 13
  • 16
  • Thanks for the response. I would like to save this JSON onto s3...so I can make a GET request later to a URL like http://s3-eu-west-1.amazonaws.com/nickr93/projects. Ideally this URL will be my JSON output. Currently that code in my save action just stops the paths from working...I am fine with rendering this as JSON onto heroku in my controller, what I need to do I think is essentially export it as JSON, if that makes sense? – biscuit_ Jan 11 '16 at 11:50
  • 1
    Well in that case the easiest would probably be to build a text file locally with the information you need, using the File class, from ruby core library (have a look [here](http://stackoverflow.com/questions/7911669/create-file-in-ruby) for an example). Once the file is created locally, you will have to upload it to S3: given that you want to put all these files in the same folder, it's probably easier to use the AWS gem directly rather than going through paperclip. – taglia Jan 11 '16 at 12:05