2017-08-08 71 views
-1

我添加更多的字段到窗体:KeyError异常/职位键:在没有找到主题:%{变更:#Ecto.Changeset

我加入他们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 

...不止一次。

他们在Postgres的,当我检查的psql:

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

我希望看到更多的错误消息,以及它崩溃的路线。它看起来像试图访问'Ecto.Changeset'结构中不存在的'changeset.topics'。 – narrowtux

+0

它在第78行崩溃,问题的最后一行。 <%= select f,:topic_id,topic_select(@ conn.assigns.topics),class:“form-control form-control-topic”,占位符:“Select topic。” %> ...我会粘贴该错误。 – RubyRube

+0

你可以发布你的控制器代码吗? – Dogbert

回答

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预计所有字段。缺少的字段会为未验证的表单返回闪存错误。