Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a controller's response from within another controller?

Rails 3.2.5. I have a series of controllers that make up a JSON API which my Backbone.js application consumes. I am now looking to create a series of controllers that will serve static content to crawlers/spiders/googlebot. I would like to get the same data from the JSON API since they are tailored to the handlebars templates I already use with my Backbone.js application but this time render them server-side.

Is there a way that I can take the JSON response from my API controllers and access them from another controller?. I know I can use curl but since the JSON API is already local to me and within the same Rails application, is there are more efficient and elegant way?

like image 496
axsuul Avatar asked Jun 04 '12 00:06

axsuul


1 Answers

No, there isn't. If you need something like this, factor the shared code inside another class ( you are probably trying to build a service ) and then use this in both controllers so you can easily share the code and then generate the JSON from the output for both of them.

Given you are using RABL for JSON generation, you should turn the pieces you want to reuse into partials and then include them in each of the specific views, just like any other rails view.

like image 139
Maurício Linhares Avatar answered Sep 22 '22 06:09

Maurício Linhares