Challenge 1: Write a Full Controller — Solution class CommentsController < ApplicationController # No render call here -- Rails implicitly renders # app/views/comments/index.html.erb after this action finishes. def index @comments = Comment.all end # No render call here either -- implicitly renders # app/views/comments/show.html.erb. def show @comment = Comment.find(params[:id]) end def new @comment = Comment.new end def create @comment = Comment.new(comment_params) if @comment.save redirect_to @comment else render :new end end def edit @comment = Comment.find(params[:id]) end def update @comment = Comment.find(params[:id]) if @comment.update(comment_params) redirect_to @comment else render :edit end end def destroy Comment.find(params[:id]).destroy redirect_to comments_path end end =begin Notes: - index and show both leave their view rendering entirely to Rails' implicit-render convention: because neither action calls render or redirect_to, Rails automatically renders the view whose filename matches the action name, inside app/views/comments/. - create and update both follow the same pattern: attempt to save, and branch based on success — redirect_to on success (a fresh request to a new URL), render :new or render :edit on failure (redisplaying the same form with whatever validation errors exist, once Chapter 7 covers validations in depth). - destroy has no view of its own at all — it performs the deletion, then always redirects, since there's nothing meaningful left to render for a resource that no longer exists. =end