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



我正在尝试通过对我的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 />

这是我通过邮递员在请求中发送的数据: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)

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

TL;DR - 您要么需要删除用户模型中的唯一性验证(通常可能会烘焙到 Devise gem 中),我不建议这样做,要么只是使用不同的电子邮件地址尝试一下,让我知道这是怎么回事。你可以编造它们进行测试,没关系。只有格式很重要。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.已检查验证,并且该用户(其电子邮件)已存在

5一.这几乎可以肯定是因为用户模型中存在唯一性验证

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

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

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

respond_to :json

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

最新更新