devise: Devise validation fails with can't be blank even if I send values properly

I have posted this issue that I have on SO as well, I am not sure if this is an issue with Devise, probably this is not, but still I can’t understand what am I doing wrong.

http://stackoverflow.com/questions/31180783/devise-validation-fails-with-cant-be-blank-even-if-i-send-values-properly

basically, I am sending values using Postman, post request with proper headers and params, but I can’t get passed this can’t be blank validation error.

If this is not an issue with Devise, I guess you can just close it. Thank you.

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)
{"user":{"email":["can't be blank"],"password":["can't be blank"],"first_name":["can't be blank"],"last_name":["can't be blank"]}}

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 22 (5 by maintainers)

Most upvoted comments

@vishalTatvaSoft I’m pretty sure that you have solved this by now, but for other people who may come here for the first time, I was fighting with this today(rails 5 api too) and what you can do is override the RegistrationsController and the sign_up_params method like this:

private

def sign_up_params
    params.require(:user).permit(:email, :password)
end

Now you can use user key in the params again.

I feel list this should be on the documentation, as I had a simular issue. The resource name of my controller was api_v1_user and should be user. My solution was to override the resource_name method like:

def resource_name
 :user
end

debugging devise_parameter_sanitizer helper, result will be

@blocks={},
 @params={"user"=>{"email"=>"rmagnum2002@gmail.com", "password"=>"admin2002"}, "format"=>:json, "controller"=>"api/users/registrations", "action"=>"create"},
 @permitted={},
 @resource_class=
  User(id: integer, email: string, encrypted_password: string, reset_password_token: string, reset_password_sent_at: datetime,...
 @resource_name=:api_user>

with @permitted={}, either

@blocks={},
 @params={"user"=>{"email"=>"rmagnum2002@gmail.com", "password"=>"admin2002"}, "format"=>:json, "controller"=>"api/users/registrations", "action"=>"create"},
 @resource_class=
  User(id: integer, email: string, encrypted_password: string, reset_password_token: string, reset_password_sent_at: datetime, ...
 @resource_name=:api_user>

both cases @resource_name is :api_user, not just user, could this be the problem?

You should tweak your devise_for accordingly.

For example, you may be able to keep things as is and pass singular: true (instead of custom controllers). Another idea is to move the devise_for call outside of your API and use the :path option to customise the path.

Check the output of rake routes to make sure you got things right.

@carlosantoniodasilva that was the problem, as I had devise routes nested under api namespace in routes I had to send params into api_user key, not user. So I probably just need to override the registration routes, no luck so far:

    devise_for :users, controllers: {
      # omniauth_callbacks: 'callbacks',
      registrations: 'api/users/registrations',
      sessions: 'api/users/sessions',
      invitations: 'api/users/invitations'
    } do
      post '/api/users' => 'api/users/registrations#create', as: :sign_up, constraints: {format: /(json)/}
    end

rake routes: user_registration POST /users(.:format) api/users/registrations#create No route matches [POST] “/api/users”

, path_names: {sign_up: '/api/users'} will generate /users/api/users(.:format)

Is there anyway to make the api/users route available to sign_up?