← Back to Upcase

Jbuilder outside of views


(Nicolo) #1

I’ve been using jbuilder for my rails json api and everything was going smoothly until I needed to send json representations of my objects to pusher.

I found some workarounds that let you render jbuilder views from a model. There is no approach that I feel great about.
see: https://github.com/rails/jbuilder/issues/84

These workarounds do not seem to be able to handle current_user outside of the view context.

Can you recommend an approach with jbuilder to render json for objects outside of views? Also, how can I set a value for current_user in these cases?


(Geoff Harcourt) #2

@nicolo,

I’m using JBuilder outside of views (I’m using them in PORO serializers rather than in a model) in my project, but I haven’t had much success with partials. To build composable serializers, I’m using JBuilder’s #to_builder and target methods whenenver I have to build a component of a larger response in a serializer.


(Chad Pytel) #3

I don’t think we’ve run into this in particular, but this gem might be worth a look. Though it doesn’t look like it’s updated for Rails 4, it might provide the additional direction needed to make this work:


(Nicolo) #4

I’ve done a bit more research on this.

Here is the stackoverflow questions where action_pusher came out of
http://stackoverflow.com/questions/6318959/rails-how-to-render-a-view-partial-in-a-model

Here is another gem that attempts render jbuilder views from a model

action_pusher
I haven’t given it too much time, but I wasn’t able to get much done with action_pusher. I’m not sure if it was a rails 4 issue or not, but the render calls were just returning nil

tilt-jbuilder
This seemed promising, but it immediately crashed because I’m rending jbuilder partials inside of partials, and it didn’t seem to be able to handle this.

I’ve basically decided I have 3 options.

option 1
Rip out jbuilder and switch to active model serializers. It seem to be able to handle the concept of scope and passing in current user. I’ve never used active model serializers before so I’m not sure it would actual do what I want.

option 2
Use jbuilder hybrid approach that involves using jbuilder views for some of the app and to_builder for any object I need to send to pusher. This would move a lot of my json display code into models or ruby objects. I’ve played around with this a little bit, and it’s pretty painful. Jbuilder views don’t seem to play all that nice with to_builder. Also, I am going to have to pass through a current_user variable down into the model through multiple associated objects to render what I want.

option 3
Build a simplified version of action_pusher which renders the jbuilder views through an ugly use of Rails view and controller objects in my models. I’ve already have found trouble passing through the context of current_user through more than one layer of partials. This approach along with action_pusher just seems dirty.

I’m going to sleep on it and then hopefully make a decision. I’m not excited about any of my current options so feel free to leave any thoughts.


(Chad Pytel) #5

Can you confirm that active_model_serializers will do what you want by converting just one thing? Thats the first thing I would recommend.


(Nicolo) #6

Do you think it would be really ugly to just pass a logged_in_user into the partial? The only problem is that this partial is a few partials down so I’d have to pass it through a couple other partials too.