Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.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_Json_Rest_Response - Fatal编程技术网

Ruby on rails 从Json响应中删除属性

Ruby on rails 从Json响应中删除属性,ruby-on-rails,json,rest,response,Ruby On Rails,Json,Rest,Response,我有一个REST服务,它的响应结构根据请求的某些属性而不同。根据发送给控制器的响应,我从代码中返回不同的类对象(不是ActiveRecord对象)。从控制器得到的响应中,某些属性需要作为响应中的头传递。但这应该从响应机构中删除。如何从类对象中删除它 假设我对控制器的响应可以是具有属性的class1对象:attr1、attr2、attr3 现在,attr3实际上应该位于响应头中,因此它可以工作: response.headers[“Headertitle”]=attr3 但是我的json响应应该只

我有一个REST服务,它的响应结构根据请求的某些属性而不同。根据发送给控制器的响应,我从代码中返回不同的类对象(不是ActiveRecord对象)。从控制器得到的响应中,某些属性需要作为响应中的头传递。但这应该从响应机构中删除。如何从类对象中删除它

假设我对控制器的响应可以是具有属性的class1对象:attr1、attr2、attr3

现在,attr3实际上应该位于响应头中,因此它可以工作: response.headers[“Headertitle”]=attr3

但是我的json响应应该只有attr1和attr2

另一种情况可能是对控制器的响应是带有attr1、attr3、attr4和attr5的class2对象。其中的attr3也会转到响应头。同样,响应不应该有属性3

attr3的值取决于某些业务规则,并根据请求而有所不同。但每个响应都需要在响应头中包含attr3,而不是在主体中

如何从响应中删除attr3

我已经试过:

format.json{render:json=>json_response.to_json(除了:[“attr3”])}

这不起作用。

您可以使用以下方法:

或者,如果您愿意,以下是正确的方法:

all = {name: "Zulh", email: "zulh@example.com", phone: "0123456789"}
=> {:name=>"Zulh", :email=>"zulh@example.com", :phone=>"0123456789"}

selected = all.except(:phone)
=> {:name=>"Zulh", :email=>"zulh@example.com"}

selected.to_json
=> "{\"name\":\"Zulh\",\"email\":\"zulh@example.com\"}"

如果您使用的是Rails 5

您可以执行以下操作:

    def send_auth_token_for_valid_login_of(user)
      render json: { user: user }, :except =>  [:password_digest, :token_created_at, :reset_password_token, :reset_password_sent_at]
    end

定义“不工作”表示attr3仍在响应。
    def send_auth_token_for_valid_login_of(user)
      render json: { user: user }, :except =>  [:password_digest, :token_created_at, :reset_password_token, :reset_password_sent_at]
    end