I am trying to send a model as json. The model has binary data in one of it’s columns. For another model I have used
format.json {self.encode64(@resource_type.data).to_json}
with success, but in that case I only wanted the data column, and not the title etc. What can I do when I want contents from several columns, where only one column’s content should be encoded with encode64?
In the following code, I don’t know where to put the self.encode64 method.
format.json { render :json => @resource.to_json(:only => [:id, :title, :data])}
How can I do this?
You have a few options here.
You could add a
data_base64method to your model that returned thedatain base-64 format and then use the:methodsoption toto_jsonin your controller:That would give you a
data_base64key in the JSON instead ofdatabut that might not be a problem.You could also use
as_jsonto get a hash and fix the encoding in the controller: