0
0
Ruby on Railsframework~10 mins

Why structure conventions matter 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 Rails controller named PostsController.

Ruby on Rails
class PostsController < [1]
  def index
    @posts = Post.all
  end
end
Drag options to blanks, or click blank then click option'
AActiveRecord
BApplicationRecord
CActionView
DApplicationController
Attempts:
3 left
💡 Hint
Common Mistakes
Using ActiveRecord as a parent class for controllers.
Confusing ApplicationRecord with ApplicationController.
2fill in blank
medium

Complete the code to render the 'show' view in the controller action.

Ruby on Rails
def show
  @post = Post.find(params[:id])
  [1]
end
Drag options to blanks, or click blank then click option'
Arender json: @post
Bredirect_to posts_path
Crender :show
Dredirect_to @post
Attempts:
3 left
💡 Hint
Common Mistakes
Using redirect instead of render when you want to display a view.
Rendering JSON when the goal is to show an HTML page.
3fill in blank
hard

Fix the error in the model by completing the validation syntax.

Ruby on Rails
class Post < ApplicationRecord
  validates :title, [1]: true
end
Drag options to blanks, or click blank then click option'
Apresence
Bexistence
Cvalid
Drequired
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'existence' which is not a valid Rails validator.
Confusing 'required' with the correct validator keyword.
4fill in blank
hard

Fill both blanks to complete the route definition for posts resource.

Ruby on Rails
Rails.application.routes.draw do
  [1] :posts, only: [:index, [2]]
end
Drag options to blanks, or click blank then click option'
Aresources
Bshow
Cget
Dpost
Attempts:
3 left
💡 Hint
Common Mistakes
Using get instead of resources for multiple routes.
Including HTTP verbs instead of action names in the only array.
5fill in blank
hard

Fill all three blanks to complete the migration creating a posts table with title and body.

Ruby on Rails
class CreatePosts < ActiveRecord::Migration[6.1]
  def change
    create_table :posts do |[1]|
      [2].string :title
      [3].text :body
    end
  end
end
Drag options to blanks, or click blank then click option'
At
Btable
Ccolumn
Drecord
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently inside the block.
Trying to use table or column as the block variable.