0

One of my active record subclasses has a method that creates a json-like representation. Is this the way to answer an array of that data?

# answer is an array
answer = MyModel.fetch()

respond_to do |format|
    format.json { render :json => answer.collect { |e| e.as_special_json } }
end

I don't think this is right. I guess I don't really understand what render :json is all about. I think this will json encode the almost-json. Is there any way to just answer the almost-json like any other json?

1 Answer 1

1

You could just render it as text:

respond_to do |format|
  format.special_json { render :text => answer.collect{ |e| e.as_special_json } }
end

Your can access add a .special_json to your url to make rails render the text. You need to map the new mime symbol "special_json" to the json mime type. Just add this line to config/initializers/mime_types.rb:

Mime::Type.register "application/json", :special_json
Sign up to request clarification or add additional context in comments.

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.