Created
December 31, 2012 14:45
-
-
Save senny/4420280 to your computer and use it in GitHub Desktop.
Simple Example how to use `accepts_nested_attributes_for`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= form_for(@post) do |f| %> | |
<% if @post.errors.any? %> | |
<div id="error_explanation"> | |
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2> | |
<ul> | |
<% @post.errors.full_messages.each do |msg| %> | |
<li><%= msg %></li> | |
<% end %> | |
</ul> | |
</div> | |
<% end %> | |
<%= f.text_field :title %> | |
<h2>Comments</h2> | |
<%= f.fields_for :comments do |fc| %> | |
<%= fc.text_field :body %> | |
<br /> | |
<% end %> | |
<div class="actions"> | |
<%= f.submit %> | |
</div> | |
<% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Comment < ActiveRecord::Base | |
attr_accessible :body | |
belongs_to :post | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Editing post</h1> | |
<%= render 'form' %> | |
<%= link_to 'Show', @post %> | | |
<%= link_to 'Back', posts_path %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Post < ActiveRecord::Base | |
attr_accessible :title, :comments_attributes | |
has_many :comments | |
accepts_nested_attributes_for :comments | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostsController < ApplicationController | |
# GET /posts | |
# GET /posts.json | |
def index | |
@posts = Post.all | |
respond_to do |format| | |
format.html # index.html.erb | |
format.json { render json: @posts } | |
end | |
end | |
# GET /posts/1 | |
# GET /posts/1.json | |
def show | |
@post = Post.find(params[:id]) | |
respond_to do |format| | |
format.html # show.html.erb | |
format.json { render json: @post } | |
end | |
end | |
# GET /posts/new | |
# GET /posts/new.json | |
def new | |
@post = Post.new | |
@post.comments.build | |
respond_to do |format| | |
format.html # new.html.erb | |
format.json { render json: @post } | |
end | |
end | |
# GET /posts/1/edit | |
def edit | |
@post = Post.find(params[:id]) | |
@post.comments.build | |
end | |
# POST /posts | |
# POST /posts.json | |
def create | |
@post = Post.new(params[:post]) | |
respond_to do |format| | |
if @post.save | |
format.html { redirect_to @post, notice: 'Post was successfully created.' } | |
format.json { render json: @post, status: :created, location: @post } | |
else | |
format.html { render action: "new" } | |
format.json { render json: @post.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# PUT /posts/1 | |
# PUT /posts/1.json | |
def update | |
@post = Post.find(params[:id]) | |
respond_to do |format| | |
if @post.update_attributes(params[:post]) | |
format.html { redirect_to @post, notice: 'Post was successfully updated.' } | |
format.json { head :no_content } | |
else | |
format.html { render action: "edit" } | |
format.json { render json: @post.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# DELETE /posts/1 | |
# DELETE /posts/1.json | |
def destroy | |
@post = Post.find(params[:id]) | |
@post.destroy | |
respond_to do |format| | |
format.html { redirect_to posts_url } | |
format.json { head :no_content } | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NestedModels::Application.routes.draw do | |
resources :posts | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment