I just started to learn elixir and phoenix.
I am trying to add comments to blog but i got this error
function Map.put/4 is undefined or private
Error is in this line (show.html.heex):
<%= render "comment_form.html", Map.put(assigns, :changeset, :action, Routes.post_post_path(@conn, :add_comment, @post)) %>
My code in post_controller.ex
def show(conn, %{"id" => id}) do
post =
id
|> Posts.get_post!
|> Repo.preload([:comments])
changeset = Comment.changeset(%Comment{}, %{})
render(conn, "show.html", post: post, changeset: changeset)
end
And comment_form.html.heex
<%= form_for @changeset, @action, fn f -> %>
<div >
<label>Name</label>
<%= text_input f, :name, class: "form-control" %>
</div>
<div >
<label>Content</label>
<%= textarea f, :content, class: "form-control" %>
</div>
<div >
<%= submit "Add comment", class: "btn btn-primary" %>
</div>
<% end %>
CodePudding user response:
You may have accidently added :changeset in the code:
- Map.put(assigns, :changeset, :action, Routes.post_post_path(@conn, :add_comment, @post))
Map.put(assigns, :action, Routes.post_post_path(@conn, :add_comment, @post))
Map.put/3 can put a new key-value pair into an existing map. Here 3 means the arity (the number of arguments) is 3.
In your case, it makes little sense to put a new assign of :changeset into the inherited assigns which already contain that key.
