class CommentsController < ApplicationController before_filter :grab_post def index @comments = Comment.find(:all) end # GET /comments/1 # GET /comments/1.xml def show @comment = Comment.find(params[:id]) end # GET /comments/new # GET /comments/new.xml def new @comment = Comment.new end # GET /comments/1/edit def edit @comment = @post.comments.find(params[:id]) end # POST /comments # POST /comments.xml def create @comment = Comment.new(params[:comment]) if (@post.comments << @comment) redirect_to post_url(@post) else render :action => :new end end # PUT /comments/1 # PUT /comments/1.xml def update @comment = @post.comments.find(params[:id]) if @comment.update_attributes(params[:comment]) redirect_to post_url(@post) else render :action => :edit end end # DELETE /comments/1 # DELETE /comments/1.xml def destroy comment = @post.comments.find(params[:id]) @post.comments.delete(comment) redirect_to post_url(@post) end private def grab_post @post = Post.find(params[:post_id]) end end