我已经设计了身份验证,当我创建用户时
curl -H'Content-Type:application / json'-H'接受: application / json'-X POST htt:// localhost:3000 / users.json -d “{'user':{'username':'sample@example.com','password':'password','password_confirmation':'password' }}“
上述请求的回复是
{ “用户”:{ “authentication_token”: “uwAqF4SG8kPirxWN35yp”, “用户名”: “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
答案 0 :(得分:1)
我认为默认行为是正确的响应 - 它返回JSON请求的新(成功)创建用户的JSON对象。
无论如何,请看一下这篇文章:Override devise registrations controller
您需要覆盖创建操作的注册控制器,例如:
def create
#custom logic here
respond_to do |format|
format.html #some logic here
format.json {"New user created successfully"}
end
end
答案 1 :(得分: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