哪里API调用在铁轨上的MVC框架项目中的红宝石去吗? [英] Where do API calls go in a ruby on rails MVC framework project?

查看:105
本文介绍了哪里API调用在铁轨上的MVC框架项目中的红宝石去吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ruby on Rails的用MVC框架的应用程序。截至目前,我已经在控制器的API调用,但不认为这是对他们正确的地方。应我所有的API调用去什么样的文件?谢谢

 高清getDetails(ID)
 api_response = HTTParty.get(base_uri,:查询=> {:DID => ID,:= DeveloperKey为GT&; devKey})
 @json_hash = api_response.parsed_response
 返回@json_hash
结束


解决方案

通过我的编码风格(和MVC的理解),外部通话将被放置在一个无表的模式。 RailsCasts 193 谈了一下这个概念,并不太笨重的语法在Rails的支持4如果你需要有code中的任何操作,该模型似乎是逻辑地而这些。移动这些方法到控制器的工作,但作为您的应用程序的增长可能会产生问题。

与外部API调用另一个需要考虑的其实是那些存储在数据库中,这绝对应该是一个模型在这一点上,所以(我)就变得更清晰,这些确实应该是在模型中。

I have an application in Ruby on Rails with mvc framework. As of now, I have API calls in the controller but don't think this is the right place for them. What kind of file should all my API calls go in? Thanks

def getDetails(id)
 api_response = HTTParty.get(base_uri, :query => {:DID => id, :DeveloperKey => devKey})
 @json_hash = api_response.parsed_response
 return @json_hash
end

解决方案

By my coding style (and understanding of MVC), external calls would be placed in a "tableless" model. RailsCasts 193 talks a bit about this concept, and a less clunky syntax is supported in Rails 4. If you need to have any manipulation of the code, the model seems like the logical place to place these. Moving those methods into the controller would work, but could create problems as your app grows.

Another consideration with external API calls is actually storing those in a database, which would should definitely be in a model at that point, so (to me) it becomes clearer that these really should be in the model.

这篇关于哪里API调用在铁轨上的MVC框架项目中的红宝石去吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆