创建时,Devise Registrations控制器“字段不能为空”

时间:2019-07-16 21:21:53

标签: ruby-on-rails api devise

我正在尝试通过对Rails api的发布请求来创建devise用户。当我提交请求时,我收到以下消息:

<h2>Sign up</h2>

<form class="new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post">
    <input name="utf8" type="hidden" value="&#x2713;" /><input type="hidden" name="authenticity_token" value="aD/tTW9DM0nXQap07ZAH6U5Uu/YP2hUA+ThM28VGKKjuZJuJw6c2JbYK6m4T737d7ue9S4RIlP1s7Y4xcMgy/g==" />
    <div id="error_explanation">
        <h2>
            2 errors prohibited this user from being saved:
        </h2>
        <ul>
            <li>Email can&#39;t be blank</li>
            <li>Password can&#39;t be blank</li>
        </ul>
    </div>


    <div class="field">
        <div class="field_with_errors"><label for="user_email">Email</label></div><br />
        <div class="field_with_errors">
            <input autofocus="autofocus" autocomplete="email" type="email" value="" name="user[email]" id="user_email" /></div>
        </div>

        <div class="field">
            <div class="field_with_errors"><label for="user_password">Password</label></div>
            <em>(6 characters minimum)</em>
            <br />
            <div class="field_with_errors">
                <input autocomplete="new-password" type="password" name="user[password]" id="user_password" /></div>
            </div>

            <div class="field">
                <label for="user_password_confirmation">Password confirmation</label><br />
                <input autocomplete="new-password" type="password" name="user[password_confirmation]" id="user_password_confirmation" />
  </div>

                <div class="actions">
                    <input type="submit" name="commit" value="Sign up" data-disable-with="Sign up" />
  </div>
</form>
<a href="/users/sign_in">Log in</a><br />

这是我在我的请求中发送的数据,这些数据是我通过邮递员制作的:http://localhost:3001/users/?email=testacct@yahoo.com&password=testpass

我也尝试使用大写的“电子邮件”和“密码”字段发送请求

服务器日志:

app/controllers/users/registrations_controller.rb:23:in `create'
Started POST "/users/?email=testacct@yahoo.com&password=[FILTERED]" for ::1 at 2019-07-16 17:13:55 -0400
Processing by Users::RegistrationsController#create as */*
  Parameters: {"email"=>"testacct@yahoo.com", "password"=>"[FILTERED]"}
   (0.2ms)  BEGIN
  ↳ app/controllers/users/registrations_controller.rb:16
  User Exists (0.4ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", ""], ["LIMIT", 1]]
  ↳ app/controllers/users/registrations_controller.rb:16
   (0.2ms)  ROLLBACK
  ↳ app/controllers/users/registrations_controller.rb:16
  Rendering /Users/.rvm/gems/ruby-2.6.1/gems/devise-4.6.2/app/views/devise/registrations/new.html.erb
  Rendered /Users/.rvm/gems/ruby-2.6.1/gems/devise-4.6.2/app/views/devise/shared/_error_messages.html.erb (1.0ms)
  Rendered /Users/.rvm/gems/ruby-2.6.1/gems/devise-4.6.2/app/views/devise/shared/_links.html.erb (0.8ms)
  Rendered /Users/.rvm/gems/ruby-2.6.1/gems/devise-4.6.2/app/views/devise/registrations/new.html.erb (7.0ms)
Completed 200 OK in 34ms (Views: 9.2ms | ActiveRecord: 5.5ms)

3 个答案:

答案 0 :(得分:0)

我将帮助您在下面剖析日志。

TL; DR-您要么需要删除用户模型中的唯一性验证(通常可能会被植入到Devise gem中),否则我不建议您 OR 使用其他电子邮件尝试请告诉我,情况如何。您可以对它们进行测试,没关系。仅格式化很重要。 Something@something.something应该没问题。

1。控制器开始处理您的请求

Processing by Users::RegistrationsController#create as */*

2。您的参数正在发送,这里是

Parameters: {"email"=>"testacct@yahoo.com", "password"=>"[FILTERED]"}

3。数据库开始保存您的用户对象

(0.2ms) BEGIN

4。注册控制器中的第16行可能是检查验证的地方

app/controllers/users/registrations_controller.rb:16

5。验证已被检查,并且该用户(他们的电子邮件)已经存在

5a。几乎可以肯定,这是因为您的用户模型具有唯一性验证

User Exists (0.4ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2 [["email", ""], ["LIMIT", 1]]

6。哦,好,不能有两个用户使用同一封电子邮件,因此记录不会保存(回滚)

app/controllers/users/registrations_controller.rb:16 (0.2ms) ROLLBACK

答案 1 :(得分:0)

看起来Devise既需要密码,又需要输入密码确认作为输入,因此在您提供的HTML中需要密码确认的字段。还尝试在POST请求中再次将密码发送到password_confirmation。它看起来应该像这样: http://localhost:3001/users/email=testacct@yahoo.com&password=testpass&password_confirmation=testpass

答案 2 :(得分:0)

我能够通过添加以下内容来解决此问题:

respond_to:json

在应用程序控制器中

并删除我添加的所有其他装置参数消毒剂。另外,我添加了devise-token-auth gem,并使用它来设置我的用户路由