2017-08-08 12 views
-1

私はフォームに複数のフィールドを追加している。中には見ら​​れないトピックがKeyError例外/ポストキー:%{チェンジ:#1 Ecto.Changeset

を私は

field :name_of_gallery, :string 
field :future_plans_title, :string 
をpost.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 

作られた2つの移行ファイル:

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 
0を

はstories.exにチェンジにそれらを追加しました

私が走った:

mix ecto.drop 
mix ecto.setup 
mix ecto.migrate 
mix run priv/repo/seeds.exs 

...回以上。 |

name_of_gallery:私はpsqlをチェックするとき

彼らはpostgresのです可変文字(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)、クラス: "form-control form-control-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はすべてのフィールドを想定します。不足しているフィールドは、未確認フォームのフラッシュエラーを返します。

関連する問題