如何为现有的Ruby on Rails 5应用程序创建REST API?

时间:2022-11-13 00:15:29

I have a Rails 5 app with ActionCable and Devise gem hosted on Heroku with 15K users. Many users have suggested to release a mobile app for the website. I want to start by creating a basic mobile app for the website.

我有一个带有ActionCable和Devise gem的Rails 5应用程序,在Heroku上托管了15K用户。许多用户建议为该网站发布移动应用程序。我想首先为网站创建一个基本的移动应用程序。

When I google, all I get is API only guide. Are there any tutorials or books or any resource that show how to create a REST API for an existing Rails app?

当我谷歌时,我得到的只是API指南。是否有任何教程或书籍或任何资源显示如何为现有的Rails应用程序创建REST API?

Can someone kindly guide on how to do this? How do I authenticate users? How do I use real-time chat? Should I use any gem? Kindly share how to start and the best practices. Thanks

有人可以指导如何做到这一点?我如何验证用户?我如何使用实时聊天?我应该使用任何宝石吗?请分享如何开始和最佳实践。谢谢

7 个解决方案

#1


3  

You could use grape gem. Check out its Rails integration section.

你可以使用葡萄宝石。查看其Rails集成部分。

Since you already have some routes in place, just pick up an unused root path for your API and mount it there:

由于您已经有一些路由,只需为您的API选择一个未使用的根路径并将其挂载到那里:

mount Twitter::API => '/api'

#2


2  

Are there any tutorials or books or any resource that show how to create a REST API for an existing Rails app?

是否有任何教程或书籍或任何资源显示如何为现有的Rails应用程序创建REST API?

Yes. This Railscast is very helpful. He goes through how to set up the basic parts of a restful API for an already functioning (albeit very simple) Rails app.

是。这个Railscast非常有用。他介绍了如何为已经运行(尽管非常简单)的Rails应用程序设置restful API的基本部分。

It's a few years old, but should still work for Rails 5.

它已经有几年了,但仍应该适用于Rails 5。

#3


1  

I think you first start with API authentication. This gem may suit your need: https://github.com/lynndylanhurley/devise_token_auth

我想你首先要从API身份验证开始。这个宝石可能适合您的需求:https://github.com/lynndylanhurley/devise_token_auth

#4


0  

I would recommend you to start looking for the JSON responses, in Rails you have everything you need to create an web application and an API with the same controllers. You need to start with 'respond_to' for JSON. To start playing around, use scaffold for controller generation and it will provide you a responder for JSON and HTML.

我建议你开始寻找JSON响应,在Rails中,你拥有创建Web应用程序和使用相同控制器的API所需的一切。你需要从'respond_to'开始为JSON。要开始玩游戏,请使用scaffold进行控制器生成,它将为您提供JSON和HTML的响应程序。

#5


0  

May be you can use activemodel serializers gem, it is part of rails 5. if you are currently not using Rails 5 api mode, you can add activemodel serializers gem to your Gemfile.

可能是你可以使用activemodel序列化程序gem,它是rails 5的一部分。如果你当前没有使用Rails 5 api模式,你可以在你的Gemfile中添加activemodel serializers gem。

may be this tutorial will help

可能是本教程会有所帮助

#6


0  

For Ruby on Rails 5 you can generate an API app using:

对于Ruby on Rails 5,您可以使用以下命令生成API应用:

$ rails new my_api --api

$ rails new my_api --api

You can also change an existing application and make it an API one, with a few minor changes in some files.

您还可以更改现有应用程序并将其设置为API,并在某些文件中进行一些小的更改。

And for your current normal Rails app, you just need to add respond_to for JSON in your current controller. Ex:

对于您当前的普通Rails应用程序,您只需要在当前控制器中为JSON添加respond_to。例如:

def index
  @tasks = Task.all
  respond_to :html, :json
end

For user authentication you can check Devise gem

对于用户身份验证,您可以检查Devise gem

#7


0  

Since you want to integrate a RESTful API with an existing Rails app, you should refer to this guide.

由于您希望将RESTful API与现有的Rails应用程序集成,因此您应该参考本指南。

It is a more user-friendly guide on how to integrate Grape (as Nic Nilov mentioned) with your Rails 5 application. This guide assumes you already have an existing Rails application.

它是一个更加用户友好的指南,介绍如何将Grape(如Nic Nilov提到的)与Rails 5应用程序集成。本指南假设您已有一个现有的Rails应用程序。

Hopefully this helps!

希望这有帮助!

#1


3  

You could use grape gem. Check out its Rails integration section.

你可以使用葡萄宝石。查看其Rails集成部分。

Since you already have some routes in place, just pick up an unused root path for your API and mount it there:

由于您已经有一些路由,只需为您的API选择一个未使用的根路径并将其挂载到那里:

mount Twitter::API => '/api'

#2


2  

Are there any tutorials or books or any resource that show how to create a REST API for an existing Rails app?

是否有任何教程或书籍或任何资源显示如何为现有的Rails应用程序创建REST API?

Yes. This Railscast is very helpful. He goes through how to set up the basic parts of a restful API for an already functioning (albeit very simple) Rails app.

是。这个Railscast非常有用。他介绍了如何为已经运行(尽管非常简单)的Rails应用程序设置restful API的基本部分。

It's a few years old, but should still work for Rails 5.

它已经有几年了,但仍应该适用于Rails 5。

#3


1  

I think you first start with API authentication. This gem may suit your need: https://github.com/lynndylanhurley/devise_token_auth

我想你首先要从API身份验证开始。这个宝石可能适合您的需求:https://github.com/lynndylanhurley/devise_token_auth

#4


0  

I would recommend you to start looking for the JSON responses, in Rails you have everything you need to create an web application and an API with the same controllers. You need to start with 'respond_to' for JSON. To start playing around, use scaffold for controller generation and it will provide you a responder for JSON and HTML.

我建议你开始寻找JSON响应,在Rails中,你拥有创建Web应用程序和使用相同控制器的API所需的一切。你需要从'respond_to'开始为JSON。要开始玩游戏,请使用scaffold进行控制器生成,它将为您提供JSON和HTML的响应程序。

#5


0  

May be you can use activemodel serializers gem, it is part of rails 5. if you are currently not using Rails 5 api mode, you can add activemodel serializers gem to your Gemfile.

可能是你可以使用activemodel序列化程序gem,它是rails 5的一部分。如果你当前没有使用Rails 5 api模式,你可以在你的Gemfile中添加activemodel serializers gem。

may be this tutorial will help

可能是本教程会有所帮助

#6


0  

For Ruby on Rails 5 you can generate an API app using:

对于Ruby on Rails 5,您可以使用以下命令生成API应用:

$ rails new my_api --api

$ rails new my_api --api

You can also change an existing application and make it an API one, with a few minor changes in some files.

您还可以更改现有应用程序并将其设置为API,并在某些文件中进行一些小的更改。

And for your current normal Rails app, you just need to add respond_to for JSON in your current controller. Ex:

对于您当前的普通Rails应用程序,您只需要在当前控制器中为JSON添加respond_to。例如:

def index
  @tasks = Task.all
  respond_to :html, :json
end

For user authentication you can check Devise gem

对于用户身份验证,您可以检查Devise gem

#7


0  

Since you want to integrate a RESTful API with an existing Rails app, you should refer to this guide.

由于您希望将RESTful API与现有的Rails应用程序集成,因此您应该参考本指南。

It is a more user-friendly guide on how to integrate Grape (as Nic Nilov mentioned) with your Rails 5 application. This guide assumes you already have an existing Rails application.

它是一个更加用户友好的指南,介绍如何将Grape(如Nic Nilov提到的)与Rails 5应用程序集成。本指南假设您已有一个现有的Rails应用程序。

Hopefully this helps!

希望这有帮助!