I’m new to Elixir and Phoenix, but I’m getting things to work after going through a tutorial on Udemy… I’m impressed by the performance and excited about exploring, but there are lots of rough spots in the documentation that make this a slow process.
For the ease of implementation, I would like to format my JSON responses using the JSON API spec (http://jsonapi.org/). It’s particularly useful because each resource can tell the client (e.g. an iOS app) where the related resources are. I haven’t been able to get any of the packages working for transforming my responses to JSON API format, so I’ve decided to just edit the JSON views… low tech. I’m going for something like this:
{
"meta": {
"pagination": {
"total_entries": 169,
"page_size": 16
}
},
"links": {
"self": "/api/somethings?page=1",
"prev": null,
"next": "/api/somethings?page=2",
"last": "/api/somethings?page=16",
"first": "/api/somethings"
},
"data": [
{
"type": "something",
"id": 1,
"attributes": {
"status": 1,
"name": "Bla bla",
"description": "bla bla bla"
},
"links": {
"self": "/api/somethings/1"
}
}
]
}
However, I’m running into problems with the links on the individual items in the collection. For example, the index.json view should list a paginated collection of resources, and thanks to the Scrivener plugin, I was able to get links in place for the collection so the client will know the links for next, prev, first, last… but how can I generate links for the individual items? The path helpers require the conn
variable, e.g.
something_path(conn, :show, something)
I can twiddle with this to thread the conn variable from the controller and through all the madness in the view module, but it feels really hacky. Does anyone have an example of doing this anywhere?