Ruby on rails 422错误:(不可处理实体)
我是新来的,所以我提前道歉如果我对我的问题的解释有问题,我使用的是Ionic v1。在前端和后端的ruby教程: 我试图创建一个用户,并将该信息存储在后端,但仍会遇到422(无法处理的实体错误)。我读过很多不同的论坛,但我被卡住了 这是我的前端:Ruby on rails 422错误:(不可处理实体),ruby-on-rails,ionic-framework,Ruby On Rails,Ionic Framework,我是新来的,所以我提前道歉如果我对我的问题的解释有问题,我使用的是Ionic v1。在前端和后端的ruby教程: 我试图创建一个用户,并将该信息存储在后端,但仍会遇到422(无法处理的实体错误)。我读过很多不同的论坛,但我被卡住了 这是我的前端: .controller('SignupCtrl', function($scope,SignupSession,$ionicPopup) { $scope.signup = function(full_name, password, repeat
.controller('SignupCtrl', function($scope,SignupSession,$ionicPopup) {
$scope.signup = function(full_name, password, repeat_password) {
var user_session = new SignupSession({ user: { full_name: full_name, password: password }});
user_session.$save(
function(data){
window.localStorage['full_name'] = full_name;
window.localStorage['password'] = password;
$location.path('/app/playlists');
},
function(err){
$ionicPopup.alert({
title: 'An error occured',
template: err["data"]["error"]
});
}
);
}
})
.factory('SignupSession', function($resource) {
return $resource(window.server_url + "/users.json") //application/vnd.api+json
})
这是我的后端:
用户控制器:
def create
user = User.new(user_params)
if user.save
render json: user, status: :created, meta: default_meta
else
render_error(user, :unprocessable_entity)
end
end
def user_params
ActiveModelSerializers::Deserialization.jsonapi_parse(params)
end
def create
data = ActiveModelSerializers::Deserialization.jsonapi_parse(params)
Rails.logger.error params.to_yaml
user = User.where(full_name: data[:full_name]).first
head 406 and return unless user
if user.authenticate(data[:password])
user.regenerate_token
render json: user, status: :created, meta: default_meta,
serializer: ActiveModel::Serializer::SessionSerializer and return
end
会话控制器:
def create
user = User.new(user_params)
if user.save
render json: user, status: :created, meta: default_meta
else
render_error(user, :unprocessable_entity)
end
end
def user_params
ActiveModelSerializers::Deserialization.jsonapi_parse(params)
end
def create
data = ActiveModelSerializers::Deserialization.jsonapi_parse(params)
Rails.logger.error params.to_yaml
user = User.where(full_name: data[:full_name]).first
head 406 and return unless user
if user.authenticate(data[:password])
user.regenerate_token
render json: user, status: :created, meta: default_meta,
serializer: ActiveModel::Serializer::SessionSerializer and return
end
这是我在谷歌浏览器上的日志和信息:
Started POST "/users.json" for 127.0.0.1 at 2017-08-04 12:40:39 -0700
Processing by UsersController#create as JSON
Parameters: {"user"=>{"full_name"=>"btejes@yahoo.com", "password"=>"[FILTERED]"}}
[1m[35m (0.1ms)[0m [1m[36mbegin transaction[0m
[1m[35m (0.1ms)[0m [1m[31mrollback transaction[0m
[active_model_serializers] Rendered ActiveModel::Serializer::ErrorSerializer with ActiveModelSerializers::Adapter::JsonApi (0.29ms)
Completed 422 Unprocessable Entity in 6ms (Views: 0.7ms | ActiveRecord: 0.2ms)
{errors: [{source: {pointer: "/data/attributes/password"}, detail: "can't be blank"},…]}
errors
:
[{source: {pointer: "/data/attributes/password"}, detail: "can't be blank"},…]
它是否与前端需要有“application/vnd.api+json”的内容类型有关?任何帮助都将不胜感激,我提前表示歉意,这篇文章篇幅较长
Ben由于某些原因,密码为空,请在发出请求时通过调试方法检查参数。谢谢,Sepastian!这很奇怪,因为我检查了日志,它显示用户和密码正在那里传递。我确实发现,当我展开时,下面的错误似乎认为用户名也是空的:{source:{pointer:“/data/attributes/full name”},detail:“cant be blank”}detail:“cant be blank”source:{pointer:“/data/attributes/full name”}指针:“/data/attributes/full name”我建议您检查方法中的参数,可能它说密码是空的,因为请求只发送“”,而不是传递的密码,请在方法之后立即使用
byebug
,并在rails服务器中查看参数由于某种原因,密码是空的,请在发出请求时通过调试方法检查参数。谢谢,Sepastian!这很奇怪,因为我检查了日志,它显示用户和密码正在那里传递。我确实发现,当我展开时,下面的错误似乎认为用户名也是空的:{source:{pointer:“/data/attributes/full name”},detail:“cant be blank”}detail:“cant be blank”source:{pointer:“/data/attributes/full name”}指针:“/data/attributes/full name”我建议您检查方法中的参数,可能它说密码是空的,因为请求只发送“”,而不是传递的密码,请在方法后面使用byebug
,并查看rails服务器中的参数