Skip to Content
Phoenix Web Development
book

Phoenix Web Development

by Brandon Richey
April 2018
Beginner to intermediate content levelBeginner to intermediate
406 pages
9h 33m
English
Packt Publishing
Content preview from Phoenix Web Development

Implementing an API Resource Show

We'll need to next a resource show API route since that will make it easier for us to demonstrate how to build custom error handlers for your APIs.

Open up lib/vocial_web/router.ex:

  # Other scopes may use custom stacks.  scope "/api", VocialWeb do    pipe_through :api    resources "/polls", Api.PollController, only: [:index, :show]  end

We'll also need to implement a Show action in lib/vocial_web/controllers/api/poll_controller.ex:

  def show(conn, %{"id" => id}) do    poll = Votes.get_poll(id)    render(conn, "show.json", poll: poll)  end

And of course our View needs to be implemented as well for the JSON to get back out to the user:

def render("show.json", %{poll: poll}) do  %{    poll: render_one(poll)  }end

When you request ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Phoenix in Action

Phoenix in Action

Geoffrey Lessel
Real-Time Phoenix

Real-Time Phoenix

Stephen Bussey

Publisher Resources

ISBN: 9781787284197Supplemental Content