Hi *, I'm writing an API for my application, but I can't figure out how to behave with the json frontend, where I need to do something like @object.image.url.
Should I write a .json view or there's some special method for this kind of problems ? I see to_json has a handy methods option that should call the method and put it in the output, but the problem here is that image is a Paperclip object (that contains also my S3 keys, so it's really not the case :p) and I need to call the url method.
Suggestions ?
ngw
In case you decide that JSON views are what you want, have a look at a
(trivial) template handler I wrote to help with just that
http://github.com/mschuerig/ruby_template_handler
Michael
Nice, thanks a lot.
What do I have to include to use it ? Right now by following the example my only result is to have a string in return.
ngw
>> Hi *, I'm writing an API for my application, but I can't figure
>> out how to behave with the json frontend, where I need to do
>> something like @object.image.url.
>> Should I write a .json view or there's some special method for
>> this kind of problems ?
>
> In case you decide that JSON views are what you want, have a look
> at a (trivial) template handler I wrote to help with just that
>
> GitHub - mschuerig/ruby_template_handler: A Rails view template handler that renders Ruby objects to JSON.
Nice, thanks a lot.
What do I have to include to use it ?
You can install the handler either as a plugin using script/plugin or as
a gem. In the latter case you have to add
config.gem "mschuerig-ruby_template_handler",
:lib => 'ruby_template_handler'
to config/environment.rb.
Right now by following the
example my only result is to have a string in return.
Well, what else do you want to return? The body of your response is
always going to be a string, in this case a string in JSON format.
Michael