Rails: Pretty print JSON without overkill -


I am using code below to display an unauthorized message in JSON:

 < Code> def Render_unuthorized # Displays unauthorized messages because the user has not passed the appropriate authentication parameter # Issues ['WWW-Authenticate'] = 'Token realm = "API"' to submit json: {error: {type: "unauthorized", message: "This page can not be reached without a valid API key." Which output:  

{"error": {"type": "unauthorized", "message": "This page can not be accessed without a valid API key."} }

So my question is: There is a way to print this message (without putting it in a different view and using some 3rd party gem ).

Edit: What is Beautiful Print?

Location properly, and well .. I think I would like to see the output:

  {"error": {"type": "unauthorized" , "Message": "This page can not be accessed without a valid API key."}}  

solution

work below using @Emelenin's answer Done For the record, what this final code looks like (because it did not include the whole thing):

  def render_unuthorized # displays unauthorized messages, because the user does not pass the # qualified authentication parameter Have done ['& Quot; - & quot; - here it is: ') Error: {type: "unauthorized", message: "self." ['WWW-Authenticate'] = 'Token realm = "API"' json: JSON.pretty_generate This page can not be accessed without a valid API key. "}}), Position: 401 end  

JSON built in this helpful method I tried it now and it works:

 > Str = '{"Error": {"Type": "Unauthorized", "Message": "This page can not be accessed without a valid API key."}}' 'JSON.pretty_generate (JSON.parse ( Str)) = & gt; "{\ N \" error \ ": {\ n \" type \ ": \" unauthorized \ ", \ n \" message \ ": \" this page without a valid API key Can not be reached. \ "\ N} \ N}"  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -