Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 设计-强制JSON响应_Ruby On Rails_Ruby_Json_Devise - Fatal编程技术网

Ruby on rails 设计-强制JSON响应

Ruby on rails 设计-强制JSON响应,ruby-on-rails,ruby,json,devise,Ruby On Rails,Ruby,Json,Devise,如何在Desive上强制响应JSON 当我的控制器需要使用身份验证时 before_filter :authenticate_user! 只有当我发布JSON数据时,响应才会使用JSON。 但是我想设计一些控制器和动作总是用JSON响应 编辑 我正在使用 :token_authenticatable 因此,我为每个呼叫传递auth_令牌: curl --data "token=xyzHtPAFGuAuFiVWsxsZ&receipt=adsfadsfsdfdsf" http://0.

如何在Desive上强制响应JSON

当我的控制器需要使用身份验证时

before_filter :authenticate_user!
只有当我发布JSON数据时,响应才会使用JSON。 但是我想设计一些控制器和动作总是用JSON响应

编辑

我正在使用

:token_authenticatable
因此,我为每个呼叫传递auth_令牌:

curl --data "token=xyzHtPAFGuAuFiVWsxsZ&receipt=adsfadsfsdfdsf" http://0.0.0.0:3000/user/receipt
但当令牌不正确时,响应是HTML重定向

<html><body>You are being <a href="http://0.0.0.0:3000/users/sign_in">redirected</a>.</body></html>
您正在被删除。

我希望这个回答是JSON,我不确定我是否理解你的问题,但这可能就是你想要的。只需将Response_添加到控制器中,如:

class MyController < ApplicationController
  respond_to :json

  ...
end
class MyController
您需要定义所需响应的内容类型

curl -X POST \
     -H "Content-Type:application/json" \
     -H "Accept:application/json \
     -d "token=xyzHtPAFGuAuFiVWsxsZ&receipt=adsfadsfsdfdsf" \
      http://0.0.0.0:3000/user/receipt

有关更多参数,请查看curl手册页。

感谢您的回复。我试过了,但反应仍然是重定向。