2

I am sending the request using postman, all the required fields are in place, however, I get the params required errors, like I send nothing when I actually do send the required params to the server.

I was reading this blog that says in order to get json reponse in devise I just need to add respond_to :json line in devise controllers, but it seems this is not enough.

I would've add more info to the question if I would know what exactly is needed.

routes:

  namespace :api, defaults: { format: :json } do
    devise_for :users, controllers: {
      registrations: 'api/users/registrations',
      sessions: 'api/users/sessions',
      invitations: 'api/users/invitations'
    }
  end

controllers:

module Api
  class Users::RegistrationsController < Devise::RegistrationsController
    respond_to :json
    layout false, only: :create

    def create
      build_resource(sign_up_params)
      resource.save
      yield resource if block_given?
      if resource.persisted?
        # valid resource
      else
        clean_up_passwords resource
        set_minimum_password_length
        render json: {user: resource.errors}
      end
    end
  end
end

class ApplicationController < ActionController::Base
  protect_from_forgery with: :null_session
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) << [:first_name, :last_name]
  end
end

request: with headers: Accept - application/json

Started POST "/api/users" for 127.0.0.1 at 2015-07-01 17:05:26 +0300
Processing by Api::Users::RegistrationsController#create as JSON
  Parameters: {"user"=>{"email"=>"usermail@gmail.com", "password"=>"[FILTERED]", "first_name"=>"firstName", "last_name"=>"lastName"}}
   (0.1ms)  BEGIN
   (0.1ms)  ROLLBACK
Completed 200 OK in 17ms (Views: 0.2ms | ActiveRecord: 3.8ms)

response:

{"user":{"email":["can't be blank"],"password":["can't be blank"],"first_name":["can't be blank"],"last_name":["can't be blank"]}}
rmagnum2002
  • 11,341
  • 8
  • 49
  • 86

0 Answers0