Complete the code to define a Rails controller named PostsController.
class PostsController < [1] def index @posts = Post.all end end
In Rails, controllers inherit from ApplicationController to get the basic controller behavior.
Complete the code to render the 'show' view in the controller action.
def show @post = Post.find(params[:id]) [1] end
The render :show command tells Rails to display the 'show' template for the current action.
Fix the error in the model by completing the validation syntax.
class Post < ApplicationRecord validates :title, [1]: true end
The correct validation to ensure a field is not empty is presence: true.
Fill both blanks to complete the route definition for posts resource.
Rails.application.routes.draw do [1] :posts, only: [:index, [2]] end
get instead of resources for multiple routes.only array.The resources method defines RESTful routes, and show is one of the common actions.
Fill all three blanks to complete the migration creating a posts table with title and body.
class CreatePosts < ActiveRecord::Migration[6.1] def change create_table :posts do |[1]| [2].string :title [3].text :body end end end
table or column as the block variable.In migrations, the block variable (commonly t) is used to define columns like t.string and t.text.