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 lib/rumbl_web/controllers/video_controller.ex, and scroll to the create action:

 def​ create(conn, %{​"​​video"​ => video_params}) ​do
 case​ Multimedia.create_video(video_params) ​do
  {​:ok​, video} ->
  conn
  |> put_flash(​:info​, ​"​​Video created successfully."​)
  |> redirect(​to:​ Routes.video_path(conn, ​:show​, video))
 
  {​:error​, %Ecto.Changeset{} = changeset} ->
  render(conn, ​"​​new.html"

Get Programming Phoenix 1.4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.