简体   繁体   中英

form_for issue in Phoenix

I am new to Phoenix\/Elixir and am trying to make a little chat app. I'm having some difficulties and hope you can help.

<%= f = form_for :chat, "#", id: "chat-form", phx_submit: :submit_message %>
  <%= text_input f, :message, placeholder: "Enter Message" %>
</form>

According to the documentation for Phoenix.HTML.Form.form_for/3 , the first argument is expected to be of Phoenix.HTML.FormData.t() type, while you are passing an atom there.

Somewhat alongside the below lines would work.

<%= f = form_for @changeset, "#", id: "chat-form", phx_submit: :submit_message %>
  <%= text_input f, :message, placeholder: "Enter Message" %>
</form>

Looks like you're falling afoul (like I just did) of the fact that building forms as you are, is what's suggested by the Phoenix docs, but you're using heex templates which enforce that the tags in your HTML are correct.

So there's two solutions here, depending on what sort of app you're writing.

  1. If you're not using LiveView, then save that template as leex instead, and it'll work as it.

  2. If you are using LiveView then there's a new way of building these forms with a form/1 function, documented here: https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.Helpers.html#form/1

Take a look at the new documentation here: https:\/\/hexdocs.pm\/phoenix_html\/Phoenix.HTML.Form.html#form_for\/3<\/a>

<%= form_for :chat, "#", id: "chat-form", phx_submit: :submit_message, fn f -> %>
  <%= text_input f, :message, placeholder: "Enter Message" %>
<% end %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM