2

I am building an App that serves as the backend for an android app as well as a rails webapp. To serve as a back end, I have built a series of api controllers ( ex: api_users contoller with actions show, update etc.) which will return a JSON/ XML output (using RABL).

In my Webapp, when it is time to update the User profile say, I simply want to call the update action of the api_users controller and parson the json output in my User's controller, parse this output and then handoff the data to my view etc. How do I go about making such calls?

A very bad workaround seems to be to treat it like any external API and call it using Net::Http etc. Can somebody help/ guide me to the recommended way of doing this?

Thanks,

Ramkumar.

lewstherin
  • 73
  • 3
  • possible duplicate of [How do I specify the format for url\_for in a nested route?](http://stackoverflow.com/questions/6367857/how-do-i-specify-the-format-for-url-for-in-a-nested-route) – Andy Feb 23 '13 at 17:41
  • When creating the link with `link_to`, specify the format option. See the duplicate question for more information. – Andy Feb 23 '13 at 17:41
  • Um...I am not trying to specify a link; I want to use one of my controllers as an API from within my App. So, essentially I want to call an action of a different controller and process the output within my controller action. – lewstherin Feb 23 '13 at 17:57
  • Calling one controller action from another controller action requires breaking the design of Rails, so it would be a workaround yes. I imagine you would be better off extracting the logic related to updating a profile into the model layer, and use that in both controller actions, or just use a single controller action for both cases. – Arctodus Feb 23 '13 at 18:11
  • It seems very strange that I should be able to call an external api, but there is no easy way to call my own on the same app. After all, I am not exactly breaking MVC here, all I am asking for is a complete request cycle to be processed. And this is just so that I can avoid repeating myself. – lewstherin Feb 23 '13 at 19:00
  • To avoid repeating yourself extract the logic as Sharagoz suggested-not sure what's wrong with that idea. – Dave Newton Feb 24 '13 at 15:25
  • This is an interesting question, I also want to do so but haven't found good solution yet. A good approach I know so far is using [Rest server + Heavy JS Client](http://stackoverflow.com/questions/10941249/separate-rest-json-api-server-and-client) like Backbone.js or Spine.js. But I considers the performance when using something like [HAML-Coffee](https://github.com/netzpirat/haml-coffee) because I love HAML in Rails :P – Blue Smith Jul 09 '13 at 03:57

1 Answers1

0

Since I did not find any simple way to do this, I decided to treat it like an external api. Only instead of going all the way to the dns and back, I decided to simply call it using off of localhost.

lewstherin
  • 73
  • 3