[英]Angular POST request sending info in parameters that I did not specify and Rails throwing errors about it
因此,我目前正在使用户使用devise在Rails应用程序上创建一个新帐户。
注册由客户端的Angular处理。
这是它的角度代码:
This method is in a controller
$scope.create = (isValid) ->
$scope.submitted = true
if isValid
UserService.create_user(user: {
name: $scope.name,
email: $scope.email,
password: $scope.password,
password_confirmation: $scope.password_confirmation
}, submit: 'Sign up', (response)->
console.log(response)
)
console.log(isValid)
#@userService.createUser(user, $scope)
else
@Fit.service 'UserService', [
'$resource', '$http', '$routeParams', 'Common',
($resource, $http, $routeParams, Common) ->
$resource '/users/:id/:options.json', { id: '@id', options: '@options' },
create_user: {
method: 'POST'
},
set_avatar: {
method: 'POST'
},
update_user: {
method: 'PATCH'
},
find: {
method: 'GET'
},
update_role: {
method: 'PATCH'
}
]
现在,当我以在服务器日志中获取此请求的方式发送请求时:
Started POST "/users.json" for 127.0.0.1 at 2015-06-28 22:45:35 +0300
Value for params[:user][:password] was set to nil, because it was one of [], [null] or [null, null, ...]. Go to http://guides.rubyonrails.org/security.html#unsafe-query-generation for more information.
Processing by Devise::RegistrationsController#create as JSON
Parameters: {"user"=>{"name"=>"foo", "email"=>"foo@bar.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "submit"=>"Sign up", "registration"=>{"user"=>{"name"=>"foo", "email"=>"foo@bar.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "submit"=>"Sign up"}}
(0.2ms) BEGIN
User Exists (0.4ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = 'foo@bar.com' LIMIT 1
(0.2ms) ROLLBACK
Completed 422 Unprocessable Entity in 5ms (Views: 0.2ms | ActiveRecord: 0.8ms)
现在,此日志有两个问题。 首先,在参数部分中,我有一个与我在user
对象下以及registration
对象下相同的值的列表。 现在我不知道那是哪里来的。
如您在图片中看到的,chrome调试器显示没有发送此类参数。 只有用户一个。 但是rails日志显示的却不同。
现在,这里的第二个问题是由于某种原因,日志抛出了有关我发送的参数的错误,并且我也得到了以下响应:
{"errors":{"password":["can't be blank"],"password_confirmation":["doesn't match Password"]}}
是什么引起这些问题? 如果将.erb用户创建与新的Devise项目一起使用,我检查了发送的内容,并且还在该位置发送了该user
对象下的所有参数。
关于registration
哈希,Devise可能正在使用wrap_parameters
或类似方法将整个user
哈希复制到对其更有用的格式上。 在快速的Google搜索中,我没有看到任何证据,但是注意到user
和registration
哈希看起来完全相同; 对我来说闻起来像wrap_parameters
(或类似)。 我知道这是一种相对普遍的行为,不应固有地引起Rails的任何混乱; 默认情况下,Rails应该只忽略它不特别了解/关心的参数。
但是无论如何,我看不到任何与registration
哈希值有关的错误消息。 我看到的唯一错误与:password
和:password_confirmation
字段的内容有关。 我会把这些当成面子:
[]
)发送params[:user][:password]
值; Rails用nil
代替[]
可以理解的。 您可以再次检查Angular是否提交了正确的password
值? password
和password_confirmation
不匹配。 鉴于上述情况,这是显而易见的: password
解释为nil
, password_confirmation
为"foobar"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.