Adding Current User's Information to a Post

Hi again! :smiley:

I’ve got user authentication set up using openmaize, I’d like to automatically fill a Post field with the @current_user USERNAME when the form is submitted (each post will belong to a user) without a form field that the user has to fill in.

In rails, I can do something like this :

@post.user =

And it will works.

Here’s the action of my post controller :

  def create(conn, %{"post" => post_params}) do
    current_user = conn.assigns.current_user
    changeset = Post.changeset(%Post{}, note_params)

    case Repo.insert(changeset) do
      {:ok, _post} ->
        |> put_flash(:info, "Post created successfully.")
        |> redirect(to: post_path(conn, :index))
      {:error, changeset} ->
        render(conn, "new.html", changeset: changeset)

I’m using current_user = conn.assigns.current_user but it doesnt work.

Thanks in advance :slight_smile:

bump bump! should have the answer you’re looking for.

Save the user data in this line:
changeset = Post.changeset(%Post{}, note_params)

You can pass the user data with the note_params to changeset, and there to fill the attributes that you are looking for.

Thank you :smiley: