gbrlcustodio

gbrlcustodio

How are you approaching rendeding ecto model association using "viewless" JSON views?

Hey everyone! It’s been a while since I last built a Phoenix app, and I noticed that Phoenix 1.7 no longer supports Phoenix views. I know I can always add Phoenix views as a dependency, but I’m trying to do things the new way. Unfortunately, I’m having a hard time finding resources for building JSON-APIs that work with newer Phoenix versions. The JSON and APIs documentation only covers simple scenarios.

So, I’m curious to know how you’re organizing your JSON views when it comes to reusing them for rendering associations like when we used to have render_one and render_many helper functions to cross-reference them. For example, let’s say we have a post with many comments. How are you reusing the rendering logic for both the /posts and /comments resources, where the post also includes its comments but rendering them referencing the CommentJSON view or something?

Are you doing something like this or extracting code that translates Ecto schemas into Elixir maps and reusing it?

defmodule HelloWeb.PostJSON do
  @doc """
  Renders a single post.
  """
  def show(%{post: post}) do
    %{data: data(post)}
  end

  defp data(%Post{} = post) do
    %{
      id: post.id,
      title: post.title,
      comments: HelloWeb.CommentJSON.index(%{comments: post.comments})[:data]
    }
  end
end

defmodule HelloWeb.CommentJSON do
  def index(%{comments: comments}) do
    %{data: for(comment <- comments, do: comment_json(comment))}
  end

  defp comment_json(%Comment{} = comment) do
    %{
      id: comment.id,
      text: comment.type
    }
  end
end

Most Liked

krasenyp

krasenyp

Just use functional programming, like in your example. In my opinion, Ecto schemas have no business in the controller.

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement