我在表单中添加了更多字段:
我将它们添加到post.ex
field :name_of_gallery, :string
field :future_plans_title, :string
将它们添加到stories.ex中的变更集中:
defp post_changeset(%Post{} = post, attrs \\ %{}) do
post
|> cast(attrs, [:title, :body, :user_id, :published, :original_post_id, :topic_id, :plan, :done_so_far_one, :done_so_far_two, :done_so_far_three, :done_so_far_one_title, :done_so_far_two_title, :done_so_far_three_title, :name_of_gallery, :future_plans_title])
|> cast_attachments(attrs, [:project_pic])
|> validate_required([:title, :body, :user_id, :topic_id, :plan, :done_so_far_one, :done_so_far_two, :done_so_far_three, :done_so_far_one_title, :done_so_far_two_title, :done_so_far_three_title, :project_pic, :name_of_gallery, :future_plans_title])
end
制作了两个迁移文件(此处只显示了一个):
defmodule Citybuilder.Repo.Migrations.AddNameOfGalleryToFields do
use Ecto.Migration
def change do
alter table (:stories_posts) do
add :future_plans_title, :string
end
end
end
我跑了:
mix ecto.drop
mix ecto.setup
mix ecto.migrate
mix run priv/repo/seeds.exs
...不止一次。
当我检查psql时,它们在postgres中:
name_of_gallery |字符变化(255)| future_plans_title |字符变化(255)
*
用户无法再保存表单。保存用户表单(之前有效)返回:
KeyError at POST /posts
key :topics not found in: %{changeset: #Ecto.Changeset<action: :insert, changes: %{body:
并且在线错误:
<%= select f, :topic_id, topic_select(@conn.assigns.topics), class: "form-control form-control-topic", placeholder: "Select topic." %>
我有一个主题选择菜单,但之前从未出现过问题。
*
整个错误消息(请求)在这里:
key :topics not found in: %{changeset: #Ecto.Changeset<action: :insert, changes: %{body: "My Project Summary", done_so_far_one: "Done So Far One Body", done_so_far_one_title: "Done So Far One Title", done_so_far_three: "Random Text to Test Form", done_so_far_three_title: "Random Text to Test Form", done_so_far_two: "Random Text to Test Form", done_so_far_two_title: "Random Text to Test Form", name_of_gallery: "Random Text to Test Form", plan: "My Project Plan", project_pic: %{file_name: "person-woman-park-music (1).jpg", updated_at: #Ecto.DateTime<2017-08-08 13:11:46>}, title: "My Project Title", topic_id: 1, user_id: 2}, errors: [future_plans_title: {"can't be blank", [validation: :required]}], data: #Citybuilder.Stories.Post<>, valid?: false>, layout: {Citybuilder.Web.LayoutView, "app.html"}, user: %Citybuilder.Auths.User{__meta__: #Ecto.Schema.Metadata<:loaded, "auths_users">, admin: false, encrypted_password: "$2b$12$MvDVJo01scAyrrBtnT1dpulbuj9uwDnS2ZezFeUhmzol7mJXRBYa6", id: 2, inserted_at: ~N[2017-08-08 12:20:13.948747], moderator: false, password: nil, password_confirmation: nil, updated_at: ~N[2017-08-08 12:20:13.970885], username: "jarvis"}}
此处的帖子和新功能的控制器代码:
def new(conn, _params) do
default_topic = Stories.get_topic!(@default_topic)
changeset = Stories.change_post(
%Citybuilder.Stories.Post{},
%{topic_id: default_topic.id}
)
render(conn, "new.html", changeset: changeset)
end
def create(conn, %{"post" => post_params}) do
case Stories.create_post(post_params, conn.assigns.user) do
{:ok, post} ->
conn
# |> put_flash(:info, "Post created! ヽ(´▽`)/")
|> redirect(to: post_path(conn, :show, post))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
答案 0 :(得分:0)
更改了这一行:
plug :set_topics when action in [:index, :new, :edit, :update, :fork]
到此:
plug :set_topics when action in [:index, :new, :edit, :create, :update, :fork]
添加了创建功能。表单现在通过编译器,但仍然存在一些验证错误。
*
修复表单验证:
我的变更集中有一些字段:
defp post_changeset(%Post{} = post, attrs \\ %{}) do
post
|> cast(attrs, [:title, :body, :user_id, :published, :original_post_id, :topic_id, :plan, :done_so_far_one, :done_so_far_two, :done_so_far_three, :done_so_far_one_title, :done_so_far_two_title, :done_so_far_three_title, :name_of_gallery, :future_plans_title])
|> cast_attachments(attrs, [:project_pic])
|> validate_required([:title, :body, :user_id, :topic_id, :plan, :done_so_far_one, :done_so_far_two, :done_so_far_three, :done_so_far_one_title, :done_so_far_two_title, :done_so_far_three_title, :project_pic, :name_of_gallery, :future_plans_title])
在form.html.eex中不存在。当表单被验证时,Phoenix / Ecto期望所有字段。缺少的字段会为未经验证的表单返回闪存错误。