Bird
Raised Fist0
Ruby on Railsframework~20 mins

MVC architecture in Rails - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Rails MVC Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What does this Rails controller action render?
Given the following Rails controller action, what will be the rendered output when a user visits the corresponding route?
Ruby on Rails
class BooksController < ApplicationController
  def show
    @book = Book.find(params[:id])
  end
end
AIt renders a JSON response of the @book object automatically.
BIt renders the show.html.erb view with access to @book instance variable.
CIt redirects to the index action of BooksController.
DIt raises an error because no render or redirect is specified.
Attempts:
2 left
💡 Hint
In Rails, if no explicit render or redirect is called, it renders the view matching the action name.
❓ state_output
intermediate
2:00remaining
What is the value of @posts after this Rails model query?
Consider the following code in a Rails controller. What will be the value of @posts?
Ruby on Rails
class PostsController < ApplicationController
  def index
    @posts = Post.where(published: true).order(created_at: :desc).limit(5)
  end
end
A@posts contains posts ordered by title alphabetically.
B@posts contains all posts regardless of published status.
C@posts contains 5 random posts from the database.
D@posts contains the 5 most recently created posts where published is true.
Attempts:
2 left
💡 Hint
Look at the where, order, and limit methods chaining.
📝 Syntax
advanced
2:00remaining
Which option correctly defines a Rails model with a validation?
Which of the following code snippets correctly defines a Rails model with a presence validation on the title attribute?
A
class Article &lt; ApplicationRecord
  validates :title, presence: true
end
B
class Article &lt; ApplicationRecord
  validates :title, presence
end
C
class Article &lt; ApplicationRecord
  validates_presence_of :title
end
D
class Article &lt; ApplicationRecord
  validate :title, presence: true
end
Attempts:
2 left
💡 Hint
Check the correct method name and syntax for validations.
❓ lifecycle
advanced
2:00remaining
What happens when a Rails model callback before_save raises an exception?
If a before_save callback in a Rails model raises an exception, what is the effect on the save operation?
AThe save operation is aborted and the exception propagates up, preventing the record from saving.
BThe save operation ignores the exception and continues saving the record.
CThe save operation retries the callback automatically before saving.
DThe save operation saves the record but logs the exception silently.
Attempts:
2 left
💡 Hint
Callbacks can halt the save process if they raise errors.
🔧 Debug
expert
3:00remaining
Why does this Rails view raise a 'undefined method' error for @user.name?
Given this view code snippet, why does it raise an error 'undefined method `name` for nil:NilClass'?
Ruby on Rails
<%= @user.name %>
A@user is set but the name method is missing in the User model.
BThe view syntax is incorrect; it should be <%= user.name %> without @.
C@user is nil because the controller did not set @user before rendering the view.
DThe error is caused by a missing database migration for the name column.
Attempts:
2 left
💡 Hint
Check if the instance variable is assigned in the controller.

Practice

(1/5)
1. In the MVC architecture of Rails, which component is responsible for handling user input and controlling the flow of the application?
easy
A. Controller
B. Model
C. View
D. Router

Solution

  1. Step 1: Understand MVC roles

    The Model manages data, the View displays data, and the Controller handles user input and application flow.
  2. Step 2: Identify the component for user input

    The Controller receives user requests and decides what to do next.
  3. Final Answer:

    Controller -> Option A
  4. Quick Check:

    Controller = handles user input [OK]
Hint: Controller manages user actions and app flow [OK]
Common Mistakes:
  • Confusing Model with Controller
  • Thinking View handles user input
  • Selecting Router as MVC component
2. Which of the following is the correct way to define a controller class in Rails?
easy
A. class UsersController inherits ApplicationController; end
B. def UsersController < ApplicationController; end
C. class UsersController < ApplicationController; end
D. controller UsersController extends ApplicationController {}

Solution

  1. Step 1: Recall Ruby class syntax

    In Ruby, classes are defined with class ClassName < ParentClass and closed with end.
  2. Step 2: Match Rails controller syntax

    Rails controllers inherit from ApplicationController using < syntax.
  3. Final Answer:

    class UsersController < ApplicationController; end -> Option C
  4. Quick Check:

    Ruby class syntax = class UsersController < ApplicationController; end [OK]
Hint: Use Ruby class inheritance syntax with < [OK]
Common Mistakes:
  • Using 'def' instead of 'class'
  • Using 'inherits' keyword (not Ruby)
  • Using JavaScript-like syntax
3. Given the following Rails controller code, what will be the rendered output when visiting /welcome/index?
class WelcomeController < ApplicationController
  def index
    @message = "Hello, Rails!"
  end
end
And the view file app/views/welcome/index.html.erb contains:
<h1><%= @message %></h1>
medium
A.

Hello, Rails!

B.

@message

C. Error: undefined variable @message
D.

Solution

  1. Step 1: Understand controller instance variable

    The controller sets @message to "Hello, Rails!" which is accessible in the view.
  2. Step 2: Evaluate view rendering

    The view uses ERB tag <%= @message %> to insert the string value inside the <h1> tag.
  3. Final Answer:

    <h1>Hello, Rails!</h1> -> Option A
  4. Quick Check:

    Instance variable passed to view = <h1>Hello, Rails!</h1> [OK]
Hint: Instance variables in controller show in views with <%= %> [OK]
Common Mistakes:
  • Using local variables instead of instance variables
  • Forgetting = in ERB tag
  • Expecting variable name printed literally
4. Identify the error in this Rails controller code snippet:
class ProductsController < ApplicationController
  def show
    @product = Product.find(params[:id])
  end

  def index
    @products = Product.all
  end
end
What is the likely cause of a runtime error when visiting /products/show without an id parameter?
medium
A. Method index should be defined before show
B. Missing id in params causes find to raise an exception
C. Instance variables cannot be used in controllers
D. Controller class name should be ProductController not ProductsController

Solution

  1. Step 1: Analyze show method usage

    The show method calls Product.find(params[:id]), which requires a valid id parameter.
  2. Step 2: Understand missing parameter effect

    If params[:id] is missing, find raises an ActiveRecord::RecordNotFound error causing a runtime failure.
  3. Final Answer:

    Missing id in params causes find to raise an exception -> Option B
  4. Quick Check:

    Missing param causes error = Missing id in params causes find to raise an exception [OK]
Hint: Always check required params before calling find [OK]
Common Mistakes:
  • Thinking method order matters
  • Believing instance variables are invalid
  • Confusing singular/plural controller names
5. You want to display a list of all users with their email addresses in a Rails app. Which MVC components should you update and how? A) Update the Model to fetch users, Controller to assign users to a variable, and View to loop and display emails. B) Update only the View to query the database and display emails. C) Update the Controller to fetch users and display emails directly without a View. D) Update the Model to display emails and Controller to fetch users.
hard
A. Update Model to display and Controller to fetch
B. Update View only to query and display
C. Update Controller only to fetch and display
D. Update Model to fetch, Controller to assign, View to display

Solution

  1. Step 1: Understand MVC responsibilities

    The Model handles data fetching, the Controller prepares data for the View, and the View displays data.
  2. Step 2: Apply MVC to user list display

    The Model fetches users, Controller assigns them to an instance variable, and View loops through to show emails.
  3. Final Answer:

    Update Model to fetch, Controller to assign, View to display -> Option D
  4. Quick Check:

    MVC separation = Update Model to fetch, Controller to assign, View to display [OK]
Hint: Model fetches, Controller assigns, View displays [OK]
Common Mistakes:
  • Putting database queries in the View
  • Displaying data directly in Controller
  • Making Model handle display logic
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard