I am trying to create a Phoenix application with generated html as well as json api responses. This is what I have done so far:
mix phoenix.gen.html User users name:string email:string
mix phoenix.gen.json Api.User users --no-model
Then I changed the alias in UserController from "alias MyApp.Api.User" to "alias Api.User". And it works! .. well mostly!
This is what I get in http response:
{"data":[{"id":1},{"id":2}]}
The problem I am trying to fix is - when I call GET on the Api, I just get a list of id's; all other useful fields are not being returned. IO.inspect tells me that controller is returning everything. So it must be the view which is filtering out fields.
However, my understanding of Elixir/Phoenix falls short here. This is what I see:
def render("index.json", %{users: users}) do
%{data: render_many(users, MyApp.Api.UserView, "user.json")}
end
My basic question is - How can I dive in render_many method and find out what is happening to fix this?
My secondary questions are:
- Is there any good resource to get html and json api's working together in Phoenix?
- What is "user.json" in the code above? With regards to "index.json", I think it is used only for pattern matching.