在创建时设计注册控制器 "fields cant be blank"

Devise Registrations Controller "fields cant be blank" on create

我正在尝试通过向我的 rails api 发送 post 请求来创建设计用户。当我提交请求时,我收到以下消息:

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

这是我在请求中发送的数据,我是通过 Postman 制作的: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" =  LIMIT   [["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)

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

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" = LIMIT [["email", ""], ["LIMIT", 1]]

6.哦,好吧,不能有两个用户使用相同的电子邮件,所以记录不保存(回滚)

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

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

我能够通过添加来解决这个问题:

respond_to :json

到应用程序控制器 并删除我添加的所有额外的设计参数消毒剂。另外我添加了 devise-token-auth gem 并用它来设置我的用户路由