#320 Jbuilder
Jbuilder provides a DSL for generating JSON. It includes a template engine which allows you to create complex responses with helpers and conditions.
- Download:
- source codeProject Files in Zip (102 KB)
- mp4Full Size H.264 Video (20.9 MB)
- m4vSmaller H.264 Video (10.5 MB)
- webmFull Size VP8 Video (11.6 MB)
- ogvFull Size Theora Video (22.7 MB)
Is it possible to do fragment caching with Jbuilder?
Good question. You can't use the
cache
helper method because that is designed to work with ERB. However you can use the read_fragment and write_fragment methods to manage the cache directly.Can you give a little more detail on this?
Late reply, but...
There's
json.cache!
, but I couldn't get that to work. I ended up doing aRails.cache.fetch([id, 'name']) do ... end
block around the entire json response in the jbuilder file.Is there any gem for generating xml representation in such kind of way?
There is XML Builder that comes bundled with Rails ("builder" gem)
check out a gem called "representative_view" https://github.com/mdub/representative_view
Check acts_as_api, really useful and modular. You'll define templates once for json and xml.
I have used
ActiveModel::Serializers::JSON
module to make a serializer/presenter class and I like this approach better than Jbuilder.+1
Is jBuilder similar to RABL? (I think it is...) If so - what are the pros/cons of jBuilder vs RABL?
Yes it is but RABL offers you JSON and XML.
Personally, I preferred RABL. I guess its up to everyone.
I just discovered RABL - me gusta!
RABL is similar, and I will likely do an episode on it in the future since it is different enough from Jbuilder. What I like about Jbuilder is its simplicity (it is just two small files). But RABL has more to offer in its interface.
RABL crashes for me on rails 3.2.1 on ruby 1.9.2
/Users/rackom/.rvm/gems/ruby-1.9.2-p290/gems/rabl-0.2.8/lib/rabl/template.rb:66:in <top (required)>: uninitialized constant ActionView::TemplateHandlers (NameError)
jbuilder on same configuration throws this error
Illegal instruction: 4
Any help?
see #157
Yeah, thanks a lot but:
I can fix it, so RABL would be in handler list, BUT in rails 3.2.2 it seems to me that it is trying to render json while requesting for html. #180
from the last time i've seen it, RABL is good with objects that are ActiveModel compliant. doesn't provide a lot of support if for example you have an Array/Hash object that you got from redis that needs to be represented as json.
The show notes have 'views/articles/show.json.builder' as the one path, but it should be '.jbuilder'
Very timely episode. I'm implementing it now!
Thanks! I was going nuts because of this :)
I was hoping you would do a railscast on Jbuilder! How about one on Grape, it's got some pretty cool features. If you wanted to do a 5min Pro on Grape I'd be ok with that :)
+1. An episode on Grape would be awesome.
Is there a way to do API versioning and inheritance with jbuilder?
I second this question. RABL has "extend" https://github.com/nesquena/rabl/wiki/Reusing-templates
How do you reference a boolean field?
If the value of a boolean field is false then the field will not be included in the json response
Good question, I am currently having the same problem.
for a quick&dirty support on jsonp with this, you can go with an after_filter:
Can Jbuilder templates be used inside of layouts? I tried creating a layout:
But unfortunately I just get
{"results":{}}
.anyone know how to make jbuilder pretty print?
Any luck with this?
Not perfect but this kinda works.
http://stackoverflow.com/questions/13128485/pretty-print-json-generated-with-a-jbuilder-template-in-rails-3-2-8
This looks like an option as well.
http://stackoverflow.com/questions/86653/how-can-i-pretty-format-my-json-output-in-ruby-on-rails/13029297#13029297
This is late, but a tip for others...
Assuming the reason is for viewing it yourself in a browser, the easiest (and general fix) is to use a nice browser plugin. There are two plugins both named "JSONView" for Chrome and Firefox. I use the Chrome one. Very nice tool for viewing JSON API responses.
I'm having trouble using Jbuilder with Rails 3.1.0, specifically my Jbuilder view (index.json.builder) is rendering empty, and throwing no errors. I've placed the issue on Stack hoping to get some help:
http://stackoverflow.com/questions/9999395/rails-3-1-jbuilder-rendering-empty-views
Has anyone else seen this behavior?
i had some problems but it was because i was trying to render a file/object not an attribute, something like:
json.id @image.id
json.name @image.name
json.address @image.image
(using carrierwave)
then even tho it was ok and not rising any error it didnt know how to render a file object, I had to do:
json.address @image.image.route
+1 for a episode on Grape
Goog question from Brandon Williams. Can Jbuilder templates be used inside of layouts?
Maximum that I have been to achieve is this:
What a name I thought this screen cast was going to be about Borland JBuilder(late 90's editor) ;)