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} ->
  |> 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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.