[英]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.