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

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source