0

I am sending an object through ajax, using the following hash:

container = { "client" => client, "status" => "success"}
render :json => container.to_json

The client is an object with token and password.

I can't change the javascript, it asks for the response in this shape:

$.ajax({
  {...}
  },
  success: function(data) {
    if (data.status == "success") { ... < do things with data.client >

I was trying to use:

container = { "client" => client.to_json(:only => [ :id, :first_name, :last_name, :address, :phone]), "status" => "success"}
render :json => container.to_json

But so the second .to_json handle the first as a string and wraps it between quotes.

Could someone has some idea to help me?

Thank you very much.

Thiago Melo
  • 1,157
  • 1
  • 14
  • 31
  • This may be a duplicate of [this question](http://stackoverflow.com/q/5395620/877472), not certain yet. Check the second answer, it may be what you're looking for. – Paul Richter Oct 10 '14 at 17:56
  • Thanks for reply Paul. I really tried to figure it out, but I couldn't. I mean, I need a specific shape in the response. The response object, lets say, [data], must has: data.client, data.status In this question it is doing like what I wrote above and that isn't working. – Thiago Melo Oct 10 '14 at 18:04
  • By now I am using a method that clear the object, but does't seem right for me. Anyway, thank you. – Thiago Melo Oct 10 '14 at 18:28

1 Answers1

1

Have you tried using slice?

container = { "client" => client.slice :id, :first_name, :last_name, :address, :phone }

http://api.rubyonrails.org/classes/Hash.html#method-i-slice

pdoherty926
  • 9,895
  • 4
  • 37
  • 68