0

I'm trying to upload an attachment using REST API on my server through a PUT request. I can do this by putting the binary file in the request body but I'd also like to save this file as an attachment to a model which uses paperclip to save attachments.

Here's my current involved class definitions:

class Cl < ActiveRecord::Base
  after_update :save_tses
  validates_associated :tses

  has_many :tses

  ...truncated...

  def save_tses
    tses.each do |ts|
      ts.save(false)
    end
  end

end

class Ts < ActiveRecord::Base
  has_attached_file :tsa, :styles => { :thumb => {:geometry => "100x141>", :format => :jpg} }, 
    :path => ":rails_root/public/system/:attachment/:id/:style/:friendly_filename",
    :url => "/system/:attachment/:id/:style/:friendly_filename"

  belongs_to :cl

  def friendly_filename
    "#{self.tsa_file_name.gsub( /[^a-zA-Z0-9_\.]/, '_')}" 
  end
end

I can save the attachments just fine using the file upload on the html page. I'd like to do this on a controller that receives the file as binary data through a PUT request.

Any suggestions?

2 Answers 2

1

Also you can you use -
https://github.com/jwagener/httmultiparty

Sign up to request clarification or add additional context in comments.

Comments

0

Got it,

# controller.rb

def add_ts
  # params[:id]
  # params[:tsa]

  @cl = Cl.find(params[:id])
  ts = @cl.tses.build(:name => "#{@cl.name}_#{Time.now.to_i}")

  ts.tsa = params[:tsa]
  if ts.save
    render :json => {:status => "OK"}
  else
    render :json => {:status => "ERROR"}
  end
end

# Test

curl -F "[email protected]" "http://host/cl/474/add_ts"
=> {"status":"OK"}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.