Complete the code to define a controller action that responds to a request.
class ArticlesController < ApplicationController def index @articles = Article.all render [1] end end
The index action typically renders the :index view to show all articles.
Complete the code to fetch a specific article by its ID in the controller.
class ArticlesController < ApplicationController def show @article = Article.[1](params[:id]) end end
where returns a collection, not a single record.all returns all records, not one.The find method fetches a record by its primary key, which is usually the ID.
Fix the error in the controller action to correctly redirect after creating an article.
def create @article = Article.new(article_params) if @article.save redirect_to [1] else render :new end end
articles_path goes to the index, not the new article.new_article_path goes back to the form.After creating an article, redirecting to its show page uses article_path(@article).
Fill both blanks to complete the controller action that updates an article and redirects properly.
def update @article = Article.find(params[:id]) if @article.[1](article_params) redirect_to [2] else render :edit end end
update_attributes is deprecated in newer Rails versions.articles_path goes to the index, not the updated article.The update method updates the record, and redirecting to article_path(@article) shows the updated article.
Fill all three blanks to complete a controller action that deletes an article and redirects to the articles list.
def destroy @article = Article.find(params[:id]) @article.[1] redirect_to [2], notice: [3] end
delete does not run callbacks, destroy is preferred.The destroy method deletes the record, then redirecting to articles_path shows the list with a notice message.