2

From what I have read, I can respond to a json request by using the following code:

respond_to do |format|
  format.json { render json: t.response }
end

My problem is that t.response is already in json format - and I think that my app is breaking because render json: is re-converting it to json. How do I avoid this, and simply return t.response.

Note that if I simply have t.response as my last line in the controller, the corresponding view is rendered - which is obviously not json.

Thanks!

EDIT: I am trying to get this working so I can send sms via tropo. If you have used tropo before, do you have any suggestions how to respond back to tropo correctly?

3 Answers 3

5

render json: just sets the content type to application/json (and calls to_json on the object passed to it if the object responds to to_json). It doesn't convert or somehow modifies your response.

http://apidock.com/rails/ActionController/Base/render

Sign up to request clarification or add additional context in comments.

1 Comment

if the object responds to to_json. It's funny, but ALL objects in Rails respond to to_json, so there's no need to perform checking. Even 42.to_json will work.
2

I'll show you how Rails deals with converting to json format. It's basically on line:

json = json.to_json(options) unless json.kind_of?(String)

So, strings aren't converted, other objects are converted to String via their to_json method. If your t.response returns a string then no conversion is performed.

Comments

1

You can render text instead

respond_to do |format|
  format.json { render text: t.response }
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.