# Json views

**URL:** <https://www.ruby-forum.com/t/json-views/173135>\
**Category:** Rails\
**Created:** [August 3, 2009, 8:07pm UTC](https://www.ruby-forum.com/t/json-views/173135 "2009-08-03T20:07:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicholas\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f17d59/32.png) [@Nicholas\_W](https://www.ruby-forum.com/u/Nicholas_W)\
**Post date:** [August 3, 2009, 8:07pm UTC](https://www.ruby-forum.com/t/json-views/173135/1 "2009-08-03T20:07:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Nicholas\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f17d59/32.png) [@Nicholas\_W](https://www.ruby-forum.com/u/Nicholas_W)\
**Post date:** [August 3, 2009, 8:11pm UTC](https://www.ruby-forum.com/t/json-views/173135/2 "2009-08-03T20:11:30Z")

</div>

On Monday 03 August 2009, Nicholas W. wrote:

> 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...](https://github.com/mschuerig/ruby_template_handler)**
>
> A Rails view template handler that renders Ruby objects to JSON. - GitHub - mschuerig/ruby\_template\_handler: A Rails view template handler that renders Ruby objects to JSON.

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)

---

<div class="post-metadata">

**Author:** ![Nicholas\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f17d59/32.png) [@Nicholas\_W](https://www.ruby-forum.com/u/Nicholas_W)\
**Post date:** [August 4, 2009, 7:04pm UTC](https://www.ruby-forum.com/t/json-views/173135/3 "2009-08-04T19:04:47Z")

</div>

Il giorno 03/ago/09, alle ore 20:10, Michael S. ha scritto:

> [GitHub - mschuerig/ruby\_template\_handler: A Rails view template handler that renders Ruby objects to JSON.](http://github.com/mschuerig/ruby_template_handler)

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

---

<div class="post-metadata">

**Author:** ![Nicholas\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f17d59/32.png) [@Nicholas\_W](https://www.ruby-forum.com/u/Nicholas_W)\
**Post date:** [August 4, 2009, 9:35pm UTC](https://www.ruby-forum.com/t/json-views/173135/4 "2009-08-04T21:35:15Z")

</div>

On Tuesday 04 August 2009, Nicholas W. wrote:

> > [GitHub - mschuerig/ruby\_template\_handler: A Rails view template handler that renders Ruby objects to JSON.](http://github.com/mschuerig/ruby_template_handler)
> 
> 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

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)
