Should I use a template to render JSON or use a controller that returns JSON in RoR?

What are my big doubts.

We have a project in RoR, and we are making it more dynamic using Ajax calls. They want me to put literal JSON in html templates and parse it with eval () in javascript, avoiding putting an exception in the controller to return the response as JSON directly.

Example: WhateverControoler # index → ​​will display views / whatever_controller / index.html.erb (JSON literal in html template):

{
"Success": "false",
"Date": ("need_login": "true")
}

What I propose to use in the WhateverControoler index #

render :json => (: success => false, :data => (:need_login => true))

Instead of putting it in a controller, they want me to use a template that Controller would normally display to show literal JSON.

/ , , - JSON redirct.

if (is_ajax) render :json[..] else redirect_to [..] end

, , .

? , .

PS: , "reply_to do | format |...", , - , ,

+3
2

, . :

  • render: json, Rails . mime, JSON.
  • .
  • JSON . JSON- , , , .
+3

, json erb , , json ( html, ). mime.

to_json , , JSON.

to_json , json, , json- (. ticket)

, , , to_json, to_json erb

+1

Source: https://habr.com/ru/post/1762538/


All Articles