What are the advantages of using a render JSON view vs.rendering JSON objects directly using json conn, %{data: is}

Practical examples and explanations are welcome.

1 Like

Here’s an example I prepared earlier: https://github.com/radar/phoenix-views-example

As the README sort of explains: the JSON rendering is done in the views, and as you can see from this it allows you to render an article in the index Action (with render_many) in the same way you’d render it in a show.

You could of course move these common bits to functions but then really you’d just be duplicating the purpose of views.

——

I think rendering in a controller is a great thing to do if you want to return something basic like:

%{result: “ok”, id: 2}

But if you wanted to do something more complicated, like my above example, you should use the views instead.

5 Likes