0
0
Ruby on Railsframework~20 mins

MVC architecture in Rails - Practice Problems & Coding Challenges

Choose your learning style9 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.