带有 REST API 的 Ruby on Rails [英] Ruby on Rails with REST API

查看:45
本文介绍了带有 REST API 的 Ruby on Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Ruby on Rails 的新手,虽然我喜欢所提供的组织和标准,但我对如何在这种特定情况下使 Rails 为我工作感到有些困惑.

I am new to the Ruby on Rails thing, and while I like the organization and standards provided, I am a little confused on how to make rails work for me in this specific case.

我有一个 Web 服务,我想与我的 Rails 应用程序一起使用.直接连接到数据库会很好,并立即为我提供使我的 Rails 应用程序工作所需的模型.

I have a Webservice that I want to use my rails application with. Making a direct connection to the database would be nice and provide me instantly with the models I need to make my Rails application work.

但是,我必须复制网络服务提供的所有逻辑(这不是微不足道的).如果我不直接连接到数据库,我将如何持久化模型(例如用户模型).

However, I would have to replicate all of the logic provided by the webservice(which isn't trivial). If I didn't make a direct connection to the database, how would I persist the models(such as a user model).

我是否必须创建一个单独的数据库来模拟服务器的数据库,但实际上从未直接与之交互?

Would I have to make a separate database that mimics the server's DB but never actually interacts with it directly?

提前致谢 - 如果您需要说明,请告诉我.

Thanks in advance -- let me know if you need clarification.

示例

  1. 我有一个可访问 URL www.mywebservice:8080.com/users/5 的 rails 应用
  2. 服务返回 JSON {name:foo,nick:bar,friend:baz}
  3. 在这一点上,我如何告诉 rails 用它刚刚得到的东西创建一个 User 对象,然后将它存储在数据库中?或者有没有办法持久化这个 JSON 对象?

推荐答案

如果您不需要 rails 的任何功能,为什么要这样做?我建议从 Sinatra 开始,然后添加您需要的库,作为 JSON, Rails 附带的 ActiveRecord(?).

Why rails if you don't need any of its features? I'd recommend start with Sinatra, then add the libraries you need, as JSON, ActiveRecord(?) that Rails ships with.

您可以连接到您想要的任何数据库,而不必使用 ActiveRecord,但是,很难理解您真正要问的是什么.这个标题与问题有什么关系?为什么直接DB?你不想实例化一个用户对象,然后在它上面做一个 user.to_json 吗?

You may connect to any database you want and you don't have to use ActiveRecord, however, it's hard to understand what you're really asking. How is this title related to the question? Why direct DB? You don't want to instantiate a User-object and then do a user.to_json on it?

亲亲!:)

这篇关于带有 REST API 的 Ruby on Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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