0
votes

I am using Rails 4.2 and devise.Request type will always be JSON.My registrations controller looks like this

class Users::RegistrationsController < Devise::RegistrationsController
  before_filter :configure_sign_up_params, only: [:create]

  def create
    if request.format(request) == "application/json"
      build_resource(sign_up_params)
      if resource.save
        render :json => {:success => "true", :message => "Thank You For Registering."}
      else
        render :json => {:success => "false", :message => resource.errors.full_messages}
      end
    else
      super
    end
  end

  protected

  def configure_sign_up_params
    devise_parameter_sanitizer.for(:sign_up) do |u|
      u.permit(:email, :password, :password_confirmation)
    end
  end

end

The parameters that show-up in the console is like :

 Parameters: {"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "registration"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}

The error it produces is that the controller doesn't recognize the email and password fields and always returns error like :

["Email can't be blank","Password can't be blank"]

Does this happen because of any foolishness that I have written in the code?Please help

1

1 Answers

-1
votes

Your configure_sign_up_params method has to be something like this to work.

def configure_sign_up_params
  devise_parameter_sanitizer.for(:registration) do |u|
    u.permit(:email, :password, :password_confirmation)
  end
end

If you can see the console content what you have posted, it contains a hash with registration as a key:

"registration"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}