2017-01-15 5 views
0

phoenix.htmlを使用して3階層+関連するネストされたフォームを作成する方法は? のようなものフェニックスフレームワークで入れ子になったinput_forフォームを作成する方法は?

todo_list has_many todo_items have_many item_commentsは1つの形式ですか?

私はTodoItemsのにhas_manyのItemCommentsとの1つの以上の関係を達成しようとしていた多くのTodoItems この blog todolistの、など

同じ例が、

TodoListモデル:

defmodule MyApp.TodoList do 
    use MyApp.Web, :model 

    schema "todo_lists" do 
    field :title, :string    

    has_many :todo_items, MyApp.TodoItem 

    timestamps 
    end 

    def changeset(model, params \\ :{}) do 
    model 
    |> cast(params, [:title]) 
    |> cast_assoc(:todo_items) 
    end 
end 

TodoItemモデル:

defmodule MyApp.TodoItem do 
    use MyApp.Web, :model 

    schema "todo_items" do 
    field :body, :string 

    belongs_to :todo_list, MyApp.TodoList 
    has_many :item_comments, MyApp.ItemComment 
    timestamps 
    end 

    def changeset(model, params \\ :{}) do 
    model 
    |> cast(params, [:body]) 
    |> cast_assoc(:item_comments) 
    end 
end 

ItemCommentモデル:

defmodule MyApp.ItemComment do 
    use MyApp.Web, :model 

    schema "item_comments" do 
    field :body, :string 

    belongs_to :todo_item, MyApp.TodoItem 

    timestamps 
    end 

    def changeset(model, params \\ :{}) do 
    model 
    |> cast(params, [:body]) 
    end 
end 

ToDoリストを作成するためのフォームが、私は私が使用しようと、コントローラは、このフォーム

<%= form_for @changeset, todo_lists_path(@conn, :create), fn f -> %>  
    <%= text_input f, :title %> 
    <%= inputs_for f, :todo_items, fn i -> %> 
    <%= text_input i, :body %> 
    <% end %> 
    <button name="button" type="submit">Create</button> 
<% end %> 

にitem_commentsを置く方法を確認していない新しいアクションではデフォルトでは空item_commentが含まれます、およびHTML形式でinputs_for/todo_items内の別のinputs_forを配置しようが、何も適切な方法は、フォームのこの種でやっていただきましたすべての

changeset = TodoList.changeset(%TodoList{todo_items: [%MyApp.TodoItem{item_comments: [%MyApp.ItemComment{}]}]}) 

で働いていますか?コントローラーの新規作成およびアクションの作成方法について説明します。

私は自分のやり方でフォーム作成の問題を解決しましたが、編集フォームではうまくいかず、誰かが私にそれを行う正しい方法を教えてくれますか?

changeset = TodoList.changeset(%TodoList{todo_items: [%MyApp.TodoItem{}, %MyApp.TodoItem{}]}) 

<%= form_for @changeset, todo_lists_path(@conn, :create), fn f -> %>  
    <%= text_input f, :title %> 
    <%= inputs_for f, :todo_items, fn i -> %> 
    <%= text_input i, :body %> 
    <%= inputs_for f, :todo_items, fn j -> %> 
     <%= text_input j, :param, name: "todo_list[todo_items][#{i.index}][item_comments][#{j.index}][body]" %> 
    <% end %> 
    <% end %> 
    <button name="button" type="submit">Create</button> 
<% end %> 
+2

正解。 –

+0

実際のスキーマを確認することも役立ちます。 –

+0

上記の質問は、あまりにも曖昧に答えています。投票に近づく。 –

答えて

2
<%= form_for @changeset, todo_lists_path(@conn, :create), fn f -> %>  
    <%= text_input f, :title %> 
    <%= inputs_for f, :todo_items, fn i -> %> 
    <%= text_input i, :body %> 
    <%= inputs_for i, :todo_items, fn j -> %> 
     <%= text_input j, :item_comments %> 
    <% end %> 
    <% end %> 
    <button name="button" type="submit">Create</button> 
<% end %> 

私たちは、あなたがすでにやってみたが何を正確に動作しないか見ていただければ幸いです

関連する問題