Rails:使用RestClient进行外部API集成(未定义的本地变量或方法“用户") [英] Rails: External API Integration using RestClient (undefined local variable or method `user')

查看:104
本文介绍了Rails:使用RestClient进行外部API集成(未定义的本地变量或方法“用户")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个数字图书馆,并且我已经完成了许多所需的功能.我目前在将数字图书馆与学习管理系统(LMS)集成时遇到问题.

I am building a digital library, and I have completed a lot of the functionalities needed. I am currently having an issue with integrating the digital library with a Learning Management System (LMS).

我已经有了使用 Devise gem 的数字图书馆的管理员身份验证系统.我的目标是允许希望访问数字图书馆的用户使用他们的学习管理系统(LMS)凭据(用户名和密码)登录数字图书馆.

I already have an admin authentication system for the digital library using the Devise gem. My goal is to allow users who want to access the digital library to login to the digital library using their Learning Management System (LMS) credentials (username and password).

已经为我提供了Login API端点和学习管理系统(LMS)的其他所需参数,并且我已经创建了用户模型,即会话控制器会话视图模板.

I have been provided with the Login API endpoint and other needed parameters of the Learning Management System (LMS), and I have created the User Model, the Sessions Controller and the Sessions View Templates.

我当前正在使用 RestClient Gem 进行API调用,并且我只想在成功调用session[:user_id] = user.id后保存登录信息,但是出现错误 undefined #SessionsController的本地变量或方法用户" .我不知道出了错.

I am currently using the RestClient Gem for the API call, and I just want to save the login information after succesful API call to the session[:user_id] = user.id, but I having an error undefined local variable or method `user' for #SessionsController. I can't figure out went wrong.

下面是我的源代码

会话控制器

require 'rest-client'

class SessionsController < ApplicationController
  def new
  end

  def create
    response = RestClient::Request.execute(
      method: :post,
      url: 'https://newapi.example.com/token',
      payload: { 'username': params[:username],
                 'password': params[:password],
                 'grant_type':'password' },
      headers: { apiCode: '93de0db8-333b-4f478-aa92-2b43cdb7aa9f' }
    )

    case response.code
    when 400
      flash.now[:alert] = 'Email or password is invalid'
      render 'new'
    when 200
      session[:user_id] = user.id
      redirect_to root_url, notice: 'Logged in!'
    else
      raise "Invalid response #{response.to_str} received."
    end
  end

  def destroy
    session[:user_id] = nil
    redirect_to root_url, notice: 'Logged out!'
  end
end

会话新视图

<p id="alert"><%= alert %></p>
<h1>Login</h1>
<%= form_tag sessions_path do %>
  <div class="field">
    <%= label_tag :username %>
    <%= text_field_tag :username %>
  </div>
  <div class="field">
    <%= label_tag :password %>
    <%= password_field_tag :password %>
  </div>
  <div class="actions">
    <%= submit_tag 'Login' %>
  </div>
<% end %>

用户模型

class User < ApplicationRecord
  has_secure_password

  validates :username, presence: true, uniqueness: true
end

任何形式的代码示例帮助将不胜感激.如果需要,我也愿意提供有关此集成的更多信息.预先谢谢你.

Any form of help with code samples will be greatly appreciated. I am also open to providing more information about this integration if required. Thank you in advance.

推荐答案

在会话控制器中,行session[:user_id] = user.id的用户是未定义的,即,您从未为user变量分配值.

In your sessions controller, the line session[:user_id] = user.id, user is undefined i.e. You never assigned a value to the user variable.

假设您的User数据库(在数字图书馆中,不是LMS)具有当前登录用户的记录,那么您应该使用以下方式:

Assuming your User database(in digital library, not LMS) have the record of user currently logging in, then you should use something like this:

when 200
  session[:user_id] = response.body.data.user.id
  redirect_to root_url, notice: 'Logged in!'

现在另一种情况是,当用户在您的LMS中注册时,然后当他访问数字图书馆应用并尝试登录时,将无法登录.因为您的数字图书馆应用程序没有用户与LMS链接或不了解LMS.因此,无论何时用户创建会话,您都需要检查用户记录是否存在于数字图书馆数据库中,如果没有创建,则需要检查.您可以执行以下操作:

Now another case, When the user signs up in your LMS, then when he visits digital library app, and tries to login, he won't be able to. Because your digital library app doesn't have the user linked with LMS or knows anything about LMS. So, whenever user creates a session, you'll need to check if User record is present in your digital library DB or not, if not create one. You can do something like this:

when 200
  if User.find(response.body.data.user.id).present?
    session[:user_id] = response.body.data.user.id
    redirect_to root_url, notice: 'Logged in!'
  else
    user = User.create(id: response.body.data.user.id, username: response.body.data.user.username, passord: params[:password], password_confirmation: params[:password])
    session[:user_id] = response.body.data.user.id
    redirect_to root_url, notice: 'Logged in!'
  end

以上方法仅供参考,如果需要,您必须添加更多验证.

The above method is just for reference, you must add more validations if required.

这篇关于Rails:使用RestClient进行外部API集成(未定义的本地变量或方法“用户")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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