-1

我已经设计了身份验证,并且当我将用户创建为

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' }}"

上述请求的响应是

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

但我希望得到回应

{"New user created successfully"}

我怎样才能改变以获得所需的响应?提前致谢。

更新

注册控制器创建方法如下,但我怎么能像你说的那样做

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
4

2 回答 2

1

我认为默认行为是正确的响应——它为 JSON 请求返回新(成功)创建的用户的 JSON 对象。

无论如何,看看这篇文章:覆盖设计注册控制器

您将希望为创建操作覆盖注册控制器,例如:

def create 
    #custom logic here
    respond_to do |format|
      format.html #some logic here
      format.json {"New user created successfully"}
    end
end
于 2012-11-24T10:49:15.683 回答
1

基于您的更新和 tw airball 的答案,代码将是

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
于 2012-11-24T16:17:45.517 回答