422错误:(不可处理的实体)

时间:2017-08-04 20:01:30

标签: ruby-on-rails ionic-framework

我是新手,所以如果我对我的问题的解释存在一些问题,我会提前道歉,我正在使用Ionic v1。在前端和后端的红宝石教程:https://www.simplify.ba/articles/2016/06/18/creating-rails5-api-only-application-following-jsonapi-specification/

我正在尝试创建用户并将该信息存储在后端,但继续遇到422(不可处理的实体错误)。我读过许多不同的论坛,但我被困了。

这是我的前端:

.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

以下是我在Google Chrome中的日志和信息:

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”有关?任何帮助将不胜感激,我提前道歉,这篇文章是长期的。

0 个答案:

没有答案