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

问题描述

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

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

我已获得学习管理系统 (LMS) 的登录 API 端点和其他所需参数,并且我创建了用户模型>会话控制器会话视图模板.

我目前正在使用 RestClient Gem 进行 API 调用,我只想在成功调用 session[:user_id] = user.id,但我有一个错误未定义的局部变量或 #SessionsController 的方法`user'.我不知道出了什么问题.

下面是我的源代码

会话控制器

需要'rest-client'类 SessionsController 

会话新视图

<h1>登录</h1><%= form_tag session_path do %><div class="field"><%= text_field_tag :用户名%>

<div class="field"><%= label_tag :密码%><%= password_field_tag :password%>

<div class="actions"><%= submit_tag '登录' %>

<%结束%>

用户模型

class User 

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

解决方案

在您的会话控制器中,行 session[:user_id] = user.id, user is undefined 即您从未分配过值到 user 变量.

假设你的 User 数据库(在数字图书馆,不是 LMS)有用户当前登录的记录,那么你应该使用这样的东西:

当 200session[:user_id] = response.body.data.user.idredirect_to root_url,注意:已登录!"

另一种情况,当用户在您的 LMS 中注册时,当他访问数字图书馆应用程序并尝试登录时,他将无法登录.因为您的数字图书馆应用程序没有将用户链接到 LMS 或对 LMS 一无所知.因此,每当用户创建会话时,您都需要检查您的数字图书馆数据库中是否存在用户记录,如果没有,则创建一个.你可以这样做:

当 200如果 User.find(response.body.data.user.id).present?session[:user_id] = response.body.data.user.idredirect_to root_url,注意:已登录!"别的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.idredirect_to root_url,注意:已登录!"结尾

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

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).

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).

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.

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.

Below is my source code

Sessions Controller

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

Sessions New View

<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 %>

User Model

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.

解决方案

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.

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!'

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 集成(未定义的局部变量或方法“用户")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 07:44
查看更多