Complete the code to define a Rails controller named PostsController.
class PostsController < [1] def index @posts = Post.all end end
The controller in Rails inherits from ApplicationController to handle web requests.
Complete the code to define a model named Post that inherits from the correct Rails class.
class Post < [1] validates :title, presence: true end
Models in Rails inherit from ApplicationRecord, which itself inherits from ActiveRecord::Base.
Fix the error in the view code to correctly display all post titles.
<ul>
<% @posts.[1] do |post| %>
<li><%= post.title %></li>
<% end %>
</ul>In Rails views, each is used to loop through collections for display.
Fill both blanks to complete the route that maps GET requests for posts to the index action.
Rails.application.routes.draw do [1] '[2]', to: 'posts#index' end
The get method defines a route for GET requests, and 'posts' is the URL path.
Fill all three blanks to complete a controller action that creates a new Post and redirects to its show page.
def create @post = Post.new([1]) if @post.[2] redirect_to [3], notice: 'Post was successfully created.' else render :new end end
The post_params method provides safe parameters, save saves the record, and @post is used to redirect to the show page.