class BooksController < ApplicationController def show @book = Book.find(params[:id]) end end
Rails by default renders the view template that matches the controller action name unless told otherwise. Here, the show action will render show.html.erb and the instance variable @book is available in the view.
class PostsController < ApplicationController def index @posts = Post.where(published: true).order(created_at: :desc).limit(5) end end
The query filters posts with published: true, orders them by newest first, and limits the result to 5 posts.
Option A uses the correct validates method with the presence: true option. Option A uses an invalid method validate with wrong arguments. Option A uses deprecated syntax but still valid in older Rails versions. Option A misses the true value for presence.
Raising an exception in a before_save callback stops the save process and the exception is raised to the caller. The record is not saved.
<%= @user.name %>
The error means @user is nil, so calling name on nil fails. This usually happens if the controller forgets to assign @user.