script

script

Views not working in json api

I have this view file .

 defmodule Admin.UserView do

   use AdminWeb, :view  

   def render("show.json", %{user: user}) do
     %{
       data: render_one(user, Admin.UserView, "user.json")
      }
   end

 def render("user.json", %{user: user}) do
   %{
     id: user.id,
     name: user.name,
    }
 end
end

I am calling it like this

         render(conn, "show.json", user: user)

It throws 500 error when i try to test it. But it works fine if I modify the code like this

def render("show.json", %{user: user}) do
  %{
     data: %{
     id: user.id,
     name: user.name,
     }
   }
end

I don’t see any thing wrong with user.json render function.

Any idea why it throws an error? May be I am missing something

Thanks

Marked As Solved

LostKobrakai

LostKobrakai

   def render("show.json", %{user: user} = assigns) do
     %{
       data: render_one(user, Admin.UserView, "user.json", assigns)
      }
   end

There’s no “implicit” assigns map, which all functions would automatically inherit somehow. You need to explicitly pass the assigns map onwards into render_one.

Also Liked

BartoszGorka

BartoszGorka

defmodule Admin.UserView do
   use AdminWeb, :view  

   def render("show.json", %{user: user}) do
     %{
        data: render_one(user, __MODULE__, "user.json", as: :user)
      }
   end

   def render("user.json", %{user: user}) do
     %{
        id: user.id,
        name: user.name,
      }
   end
end

And in controller render(conn, Admin.UserView, "show.json", %{user: user})

IMO this should help you - render should be as render/4 with conn, view, action, parameters details.

Where Next?

Popular in Questions Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
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
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement