0
0
Ruby on Railsframework~20 mins

Route definition in routes.rb in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rails Routing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this route helper?
Given the following route definition in routes.rb, what URL will posts_path generate?

Rails.application.routes.draw do
  resources :posts, only: [:index, :show]
end
A"/posts"
B"/posts/index"
C"/posts/show"
D"/post"
Attempts:
2 left
💡 Hint
The posts_path helper corresponds to the index action URL for posts.
📝 Syntax
intermediate
2:00remaining
Which route definition correctly creates a nested resource?
You want to define routes so that comments belong to posts, accessible via URLs like /posts/:post_id/comments. Which option correctly defines this in routes.rb?
A
resource :posts do
  resource :comments
end
B
resources :posts do
  resources :comments
end
C
resources :comments do
  resources :posts
end
Dresources :posts, :comments
Attempts:
2 left
💡 Hint
Nested resources are defined by placing one resources block inside another.
🔧 Debug
advanced
2:00remaining
Why does this route cause a routing error?
Given this route definition:
get 'profile/:id', to: 'users#show'

And this URL request: /profile
What error will Rails raise?
AArgumentError
BActionController::UrlGenerationError
CNoMethodError
DActionController::RoutingError
Attempts:
2 left
💡 Hint
The URL is missing a required parameter defined in the route.
state_output
advanced
2:00remaining
What is the HTTP verb and path for this custom route?
Consider this route in routes.rb:
post 'subscribe', to: 'newsletters#subscribe'

What HTTP verb and path does this route respond to?
APOST /subscribe
BDELETE /subscribe
CPUT /subscribe
DGET /subscribe
Attempts:
2 left
💡 Hint
The first word in the route definition is the HTTP verb.
🧠 Conceptual
expert
3:00remaining
How many routes are created by this line?
What is the total number of routes generated by this line in routes.rb?
resources :articles, only: [:index, :show, :create, :update, :destroy]
A3
B7
C5
D6
Attempts:
2 left
💡 Hint
Each action in the only list creates one route.