【问题标题】:Rails 6.0.0 TypeError - no implicit conversion of nil into String:Rails 6.0.0 TypeError - 没有将 nil 隐式转换为字符串:
【发布时间】:2020-03-15 04:51:11
【问题描述】:

人们,所以我在尝试进行后 API 调用时遇到了这个问题。 情况是这样的: 我有一个大型应用程序,学生可以在其中注册,以便我可以管理他们并通过该学生管理应用程序与他们联系。 现在我创建了一个较小的应用程序,学生可以在其中完成作业,我们称之为家庭作业应用程序; 我正在登录部分对更大的应用程序进行发布 API 调用,因此学生不必再次注册,他们可以在两个应用程序中使用相同的信息。 我收到内部服务器错误 500;在终端中,错误是:

TypeError - no implicit conversion of nil into String:
 app/controllers/api/sessions_controller.rb:12:in `create'

这是学生管理应用中会话控制器中的创建操作:

  def create
    unless request.format == :json
      sign_out
      render status: 406, json: { message: "JSON requests only." } and return
    end
    resource = warden.authenticate!(auth_options)

    if resource.blank?
      render status: 401, json: { response: "Access denied." } and return
    end

    sign_in(resource_name, resource)
    respond_with resource, location:
    after_sign_in_path_for(resource) do |format|
      format.json { render json:
        {
          success: true, jwt: current_token, response: "Authentication successful"
        }
      }
    end

  end

最重要的是使用非常简单的 html 表单的 api 调用: ps:我在本地运行项目,这就是为什么 url 被定义为 localhost

<form class="login-form">
  <input type="email" class="mail mail-input" placeholder=" Email"><br>
  <input type="password" class="password pw-input" placeholder=" Password"><br>
  <button class="login" type="button" name="button">Login</button>
</form>

<script>

var login = document.querySelector(".login")
login.addEventListener("click", function(){
  var email = document.querySelector(".mail")
  var password = document.querySelector(".password")
  let loginValues = {
    email: email.value,
    password: password.value
  }
  $.post (
   "http://localhost:4000/api/login.json",
    {
      api_user: {
        email: email.value,
        password: password.value
      }
    },
    function (response){
      console.log(response)
      console.log('in');
      window.location.replace("http://localhost:3000/")
    }
  )
})

</script>

【问题讨论】:

    标签: ruby-on-rails api authentication


    【解决方案1】:

    你可以尝试替换这个吗:

    api_user: {
      email: email.value,
      password: password.value
    }
    

    有了这个:

    user: {
      email: email.value,
      password: password.value
    }
    

    告诉我它是否有效?

    更新,基于 cmets:

    我真的不知道你在上面的代码中做了什么,代码真的很乱......尝试替换这个:

    respond_with resource, location:
    after_sign_in_path_for(resource) do |format|
      format.json { render json:
        {
          success: true, jwt: current_token, response: "Authentication successful"
        }
      }
    end
    

    用这个:

    respond_with resource do |format|
      format.json { render json:
        {
          success: true, jwt: current_token, response: "Authentication successful"
        }
      }
    end
    

    【讨论】:

    • 它有点工作。这是控制台中显示的内容,但它实际上并没有让我登录,也没有将我重定向到我想要的位置。由 Api::SessionsController#new 处理为 JSON 参数:{"user"=>{"email"=>"test@test.com", "password"=>"[FILTERED]"}} 渲染设计/会话/新.json 渲染的 devise/sessions/new.json (0.3ms) 在 6ms 内完成 200 OK (Views: 4.7ms | ActiveRecord: 0.0ms)
    • 您确定您有拥有这些凭据的用户吗?您如何处理失败的身份验证?
    • 100% 确定。我有一个无效令牌的方法,并且在我上面发布的控制器中还有一些其他失败的身份验证操作。 def invalid_auth_token respond_to do |format| format.html { redirect_to sign_in_path,错误:'登录无效或过期' } format.json { head 401 } end end
    • 当 api 调用为 api_user:resource =warden.authenticate!(auth_options);但是当我将其更改为用户时没有错误,但也没有重定向
    • 我更新了答案,我不确定你的代码是如何工作的,试试我添加的更改,看看你是否有新的东西。
    猜你喜欢
    • 1970-01-01
    • 2019-03-11
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多