简体   繁体   English

ruby on rails rest api 使用 ruby​​ 登录页面

[英]ruby on rails rest api with ruby for login page

我想在 Android 移动应用程序中使用 ruby​​ on rails 后端为登录页面实现一个 ruby​​ api,因为我是新手,请帮助我完成我的任务,请告诉我实现 API 的方法提前致谢

Firstly there is no point to put -ve mark on this question, he is new in ruby, we can help him.首先,在这个问题上打 -ve 标记是没有意义的,他是 ruby​​ 新手,我们可以帮助他。

step 1: To make an api you have to deal with controllers and models there is no view for an api.第 1 步:要制作 api,您必须处理控制器和模型,没有 api 的视图。

step 2: since you are new in rails read about rails routing, let me give you a brief idea, the router simply decide if a request comes on a url which controller and which action should trigger.第 2 步:由于您是 Rails 新手,阅读了 Rails 路由,让我给您一个简短的想法,路由器只需决定请求是否来自 url,哪个控制器和哪个动作应该触发。

step 3: Once your action in the controller is trigger write your logic their or call a method in a model.第 3 步:一旦您在控制器中的操作被触发,请编写您的逻辑或在模型中调用一个方法。

Step 4: Once you have done CRUD operation from the model return the response to the controller.第 4 步:从模型完成 CRUD 操作后,将响应返回给控制器。

step 5 : Depending on the format weather the client want a xml or json encode it in xml/json and render it from the controller.第 5 步:根据格式天气,客户端需要xmljson编码为xml/json并从控制器呈现。

dats it :) hope it helps you dats it :) 希望它可以帮助你

I don't want to quote the great thinkers, but the answer is: It depends!我不想引用伟大的思想家,但答案是:视情况而定!

You can either go it simple and use BASIC AUTHENTICATION for your app, or you can go the more-complex road of using other technologies, such as JWT (Json Web Token) or OAuth or whatnot...您可以简单地为您的应用程序使用 BASIC AUTHENTICATION,也可以走更复杂的道路,使用其他技术,例如 JWT(Json Web Token)或 OAuth 或诸如此类...

I assume you are at just starting web development so I would suggest you start simple by using BASIC AUTH (over some https connection).我假设您刚刚开始 Web 开发,所以我建议您通过使用 BASIC AUTH(通过某些 https 连接)开始简单。

For the Rails part you just include the following directive in the controller you want protected:对于 Rails 部分,您只需在要保护的控制器中包含以下指令:

http_basic_authenticate_with name: "username", password: "secret", except: :index

So a simple controller would look like this:所以一个简单的控制器看起来像这样:

class PostsController < ApplicationController
  http_basic_authenticate_with name: "dhh", password: "secret", except:  :index

  def index
    render plain: "Everyone can see me!"
  end

  def edit
    render plain: "I'm only accessible if you know the password"
  end
end

This example is directly copied from here: http://api.rubyonrails.org/classes/ActionController/HttpAuthentication/Basic.html这个例子是直接从这里复制的: http : //api.rubyonrails.org/classes/ActionController/HttpAuthentication/Basic.html

On the client side you need to provide the basic authentication credentials in the HTTP header like this:在客户端,您需要在 HTTP 标头中提供基本身份验证凭据,如下所示:

"Authorization: Basic dXNlcm5hbWU6c2VjcmV0"

using BASE64 to encode username and password like this (in javascript):使用 BASE64 像这样编码用户名和密码(在 javascript 中):

encoded = btoa(username + ":" + password); 

The basics are covered here: https://en.wikipedia.org/wiki/Basic_access_authentication此处介绍了基础知识: https : //en.wikipedia.org/wiki/Basic_access_authentication

From there you can go and implement a simple username/password screen and provide the so generated header in all subsequent calls.从那里你可以去实现一个简单的用户名/密码屏幕,并在所有后续调用中提供这样生成的标题。

I hope this gets you started into the right direction.我希望这能让你开始走向正确的方向。 Don't shy away from asking and keep learning!不要回避提问并继续学习!

Eventually you may want to move on to more sophisticated access-token based authentication methods.最终,您可能希望转向更复杂的基于访问令牌的身份验证方法。

Best regards, Steviee最好的问候,史蒂维

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

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