-1

I have devise for authentication and when i create users as

curl -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST htt://localhost:3000/users.json -d "{'user' : { 'username' : 'sample@example.com', 'password' : 'password', 'password_confirmation' : 'password' }}"

the response for the above request is

{"user":{"authentication_token":"uwAqF4SG8kPirxWN35yp", "username":"sample@example.com"}}

but i want the response to be

{"New user created successfully"}

how can i change so as to get the required response? Thanks in advance.

UPDATE

registration controller create method is as follows but how could i do it as you have said

build_resource

            if resource.save
                if resource.active_for_authentication?
                    set_flash_message :notice, :signed_up if is_navigational_format?
                    sign_in(resource_name, resource)
                    respond_with resource, :location => after_sign_up_path_for(resource)
                    else
                    set_flash_message :notice, :inactive_signed_up, :reason => inactive_reason(resource) if is_navigational_format?
                    expire_session_data_after_sign_in!
                    respond_with resource, :location => after_inactive_sign_up_path_for(resource)
                end
                else
                clean_up_passwords(resource)
                respond_with_navigational(resource) { render_with_scope :new }
            end
logesh
  • 2,572
  • 4
  • 33
  • 60

2 Answers2

1

I think the default behaviour is a correct response -- it is returning the JSON object of new (successfully) created user for a JSON request.

Anyway, take a look at this post: Override devise registrations controller

You will want to override the registrations controller for the create action something like:

def create 
    #custom logic here
    respond_to do |format|
      format.html #some logic here
      format.json {"New user created successfully"}
    end
end
Community
  • 1
  • 1
tw airball
  • 1,359
  • 11
  • 12
1

Building on your update and the answer by tw airball, the code would be

respond_to do |format|
  if resource.save
    if resource.active_for_authentication?
      set_flash_message :notice, :signed_up if is_navigational_format?
      sign_in(resource_name, resource)
      format.html { respond_with resource, :location => after_sign_up_path_for(resource) }
    else
      set_flash_message :notice, :inactive_signed_up, :reason => inactive_reason(resource) if is_navigational_format?
      expire_session_data_after_sign_in!
      format.html { respond_with resource, :location => after_inactive_sign_up_path_for(resource) }
    end
    format.json { render json: flash } # respond with the standard devise flash message
  else
    clean_up_passwords(resource)
    format.html { respond_with_navigational(resource) { render_with_scope :new } }
    format.json { render json: "User not created" }
  end
end
Felix
  • 674
  • 5
  • 16