Answers for "phoenix nested forms"

C++
0

phoenix nested forms

mix phx.new nested_forms;
cd nested_forms;
mix phx.gen.html Users User users name:string;
mix phx.gen.html Posts Post posts body:string;

# Add resources "/users", UserController to router
# Add belongs_to :user, NestedForms.Forms.User to post.ex schema
# Add has_many :posts, NestedForms.Posts.Post to user.ex schema
# Replace the user_controller "new" action with

    changeset = Users.change_user(%User{posts: [
      %NestedForms.Posts.Post{}
    ]})

mix ecto.create;
mix ecto.migrate;
mix phx.server;

# Add to lib/nested_forms_web/templates/user/form.html.eex right above the save div
     <%= inputs_for f, :posts, fn p -> %>
        <div class="form-group">
            <%= label p, :body, class: "control-label" %>
            <%= text_input p, :body, class: "form-control" %>
            <%= error_tag p, :body %>
        </div>
    <% end %>

# Open http://localhost:4000/users

# Reference: https://gist.github.com/mjrode/c2939ee7786b157aab131761c8fb89a9
Posted by: Guest on August-11-2021

Browse Popular Code Answers by Language