Complete the code to define an action method named show in a Rails controller.
class ProductsController < ApplicationController def [1] # code to show a product end end
display.show with index which lists all items.The show method is a standard Rails action used to display a single resource.
Complete the code to render the index view in the index action method.
class ArticlesController < ApplicationController def index @articles = Article.all [1] end end
redirect_to instead of render when showing a view.'show'.Using render :index explicitly tells Rails to show the index view template.
Fix the error in the create action by completing the code to save a new Post.
class PostsController < ApplicationController def create @post = Post.new(post_params) if @post.[1] redirect_to @post else render :new end end end
save! which raises errors instead of returning false.create which is a class method, not an instance method.The save method attempts to save the record and returns true or false depending on success.
Fill both blanks to define a destroy action that finds a User by id and deletes it.
class UsersController < ApplicationController def destroy @user = User.[1](params[:[2]]) @user.destroy redirect_to users_path end end
find_by without specifying the correct key.user_id instead of id in params.find fetches a record by its primary key, which is usually id.
Fill all three blanks to define an update action that finds a Comment, updates it with permitted params, and redirects to the comment.
class CommentsController < ApplicationController def update @comment = Comment.[1](params[:[2]]) if @comment.[3](comment_params) redirect_to @comment else render :edit end end end
save instead of update which requires manual attribute assignment.comment_id instead of id.find locates the record by id, and update applies changes and saves if valid.