简体   繁体   English

如何从另一个控制器中获取控制器的响应?

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

Rails 3.2.5 . Rails 3.2.5 I have a series of controllers that make up a JSON API which my Backbone.js application consumes. 我有一系列控制器组成了我的Backbone.js应用程序消耗的JSON API。 I am now looking to create a series of controllers that will serve static content to crawlers/spiders/googlebot. 我现在正在寻找创建一系列控制器,为爬虫/蜘蛛/ 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. 我想从JSON API获取相同的数据,因为它们是针对我已经在Backbone.js应用程序中使用的把手模板而定制的,但这次将它们渲染到服务器端。

Is there a way that I can take the JSON response from my API controllers and access them from another controller? 有没有办法可以从我的API控制器获取JSON响应并从另一个控制器访问它们? . 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? 我知道我可以使用curl但由于JSON API已经是我本地的并且在同一个Rails应用程序中,是否有更高效和优雅的方式?

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. 如果您需要这样的东西,请将共享代码考虑在另一个类中(您可能正在尝试构建服务 ),然后在两个控制器中使用它,这样您就可以轻松共享代码,然后从输出中为这两个代码生成JSON 。

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. 鉴于您正在使用RABL生成JSON,您应该将要重用的部分转换为部分 ,然后将它们包含在每个特定视图中,就像任何其他rails视图一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM