Skip to Main Content
Programming Phoenix
book

Programming Phoenix

by Chris McCord, Bruce Tate, Jose Valim
April 2016
Beginner to intermediate content levelBeginner to intermediate
300 pages
6h 58m
English
Pragmatic Bookshelf
Content preview from Programming Phoenix

Managing Related Data

Our generated video controller gave us the CRUD basics, but as with any generated code, we’re going to need to tailor it to our needs. We want to link videos with users for this social platform. To do so, we need to grab the current user from the connection and scope our operations against the user. Open up your web/controllers/video_controller.ex, and let’s take a look at the new action:

 def​ new(conn, _params) ​do
  changeset = Video.changeset(%Video{})
  render(conn, ​"​​new.html"​, ​changeset:​ changeset)
 end

We need to change it so the video is built with the user_id pointing to the id of the user currently stored in the connection at conn.assigns.current_user. We know that the build_assoc function in Ecto

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

Programming Elm

Programming Elm

Jeremy Fairbank
Real-Time Phoenix

Real-Time Phoenix

Stephen Bussey

Publisher Resources

ISBN: 9781680501926Errata Page