API 调用在 ruby​​ on rails MVC 框架项目中在哪里? [英] Where do API calls go in a ruby on rails MVC framework project?

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

问题描述

我在 Ruby on Rails 中有一个带有 mvc 框架的应用程序.到目前为止,我在控制器中有 API 调用,但不认为这是适合他们的地方.我所有的 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

推荐答案

根据我的编码风格(以及对 MVC 的理解),外部调用将被置于无表"模型中.RailsCasts 193 谈到了这个概念,并且 Rails 4 支持不那么笨拙的语法. 如果您需要对代码进行任何操作,模型似乎是放置这些的合乎逻辑的地方.将这些方法移到控制器中是可行的,但可能会随着您的应用的增长而产生问题.

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.

外部 API 调用的另一个考虑因素实际上是将它们存储在数据库中,那时它肯定应该在模型中,因此(对我而言)这些确实应该在模型中变得更加清晰.

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 调用在 ruby​​ on rails MVC 框架项目中在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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