2017-08-08 18 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預計所有字段。缺少的字段會爲未驗證的表單返回閃存錯誤。