Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Minimal application using accepts_nested_attributes_for

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 app
Octocat-spinner-32 config
Octocat-spinner-32 db
Octocat-spinner-32 doc
Octocat-spinner-32 lib
Octocat-spinner-32 log
Octocat-spinner-32 public
Octocat-spinner-32 script
Octocat-spinner-32 test
Octocat-spinner-32 vendor
Octocat-spinner-32 .gitignore
Octocat-spinner-32 Gemfile
Octocat-spinner-32 Gemfile.lock
Octocat-spinner-32 README.md
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 Rakefile
Octocat-spinner-32 config.ru
README.md

Demo of accepts_nested_attributes_for

Rails 3.2.1

Setup

rails new accepts_nested_attributes_for-demo
cd accepts_nested_attributes_for-demo
bundle exec rails generate scaffold Post title:string content:text
bundle exec rails generate model Comment content:text post:references
bundle exec rake db:create db:migrate

Edit app/models/post.rb

class Post < ActiveRecord::Base
  has_many :comments, :dependent => :destroy
  accepts_nested_attributes_for :comments, allow_destroy: true
end

Continue setup…

bundle exec rake db:seed

Edit app/views/posts/_form.html.erb add nested comments fields in the post form

<%= f.fields_for :comments do |comment_form| %>
  <%= comment_form.text_area :content  %>
<% end %>

Start server

bundle exec rails server
open http://localhost:3000/posts

Then edit the post comments and submit the form.

Something went wrong with that request. Please try again.