0
0
Ruby on Railsframework~10 mins

Why controllers handle requests in Ruby on Rails - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a controller action that responds to a request.

Ruby on Rails
class ArticlesController < ApplicationController
  def index
    @articles = Article.all
    render [1]
  end
end
Drag options to blanks, or click blank then click option'
A:edit
B:show
C:new
D:index
Attempts:
3 left
💡 Hint
Common Mistakes
Using :show or :new instead of :index causes the wrong view to render.
Forgetting to render a view leaves the response empty.
2fill in blank
medium

Complete the code to fetch a specific article by its ID in the controller.

Ruby on Rails
class ArticlesController < ApplicationController
  def show
    @article = Article.[1](params[:id])
  end
end
Drag options to blanks, or click blank then click option'
Awhere
Ball
Cfind
Dfind_by
Attempts:
3 left
💡 Hint
Common Mistakes
Using where returns a collection, not a single record.
Using all returns all records, not one.
3fill in blank
hard

Fix the error in the controller action to correctly redirect after creating an article.

Ruby on Rails
def create
  @article = Article.new(article_params)
  if @article.save
    redirect_to [1]
  else
    render :new
  end
end
Drag options to blanks, or click blank then click option'
Aarticle_path(@article)
Bnew_article_path
Carticles_path
Dedit_article_path(@article)
Attempts:
3 left
💡 Hint
Common Mistakes
Redirecting to articles_path goes to the index, not the new article.
Redirecting to new_article_path goes back to the form.
4fill in blank
hard

Fill both blanks to complete the controller action that updates an article and redirects properly.

Ruby on Rails
def update
  @article = Article.find(params[:id])
  if @article.[1](article_params)
    redirect_to [2]
  else
    render :edit
  end
end
Drag options to blanks, or click blank then click option'
Aupdate
Bupdate_attributes
Carticle_path(@article)
Darticles_path
Attempts:
3 left
💡 Hint
Common Mistakes
Using update_attributes is deprecated in newer Rails versions.
Redirecting to articles_path goes to the index, not the updated article.
5fill in blank
hard

Fill all three blanks to complete a controller action that deletes an article and redirects to the articles list.

Ruby on Rails
def destroy
  @article = Article.find(params[:id])
  @article.[1]
  redirect_to [2], notice: [3]
end
Drag options to blanks, or click blank then click option'
Adestroy
Barticles_path
C'Article was successfully deleted.'
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using delete does not run callbacks, destroy is preferred.
Forgetting to redirect leaves the user on the deleted article page.