Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define RESTful routes for a resource named 'articles'.
Ruby on Rails
Rails.application.routes.draw do
resources :[1]
end Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using singular form instead of plural.
Using unrelated resource names.
✗ Incorrect
In Rails, the resources method expects the plural form of the resource name to generate RESTful routes.
2fill in blank
mediumComplete the code to add only the 'index' and 'show' actions to the resource routes.
Ruby on Rails
Rails.application.routes.draw do
resources :products, only: [:[1]]
end Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of an array.
Including actions not intended.
✗ Incorrect
The 'only' option takes an array of actions to limit the routes generated. Here, 'index' and 'show' are the two actions.
3fill in blank
hardFix the error in the code to correctly nest comments inside posts.
Ruby on Rails
Rails.application.routes.draw do
resources :posts do
resources :[1]
end
end Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using singular form for nested resources.
Using incorrect resource names.
✗ Incorrect
Nested resources should use the plural form to generate correct RESTful routes for the nested resource.
4fill in blank
hardFill both blanks to define a custom member route named 'preview' for articles.
Ruby on Rails
Rails.application.routes.draw do
resources :articles do
[1] :preview, on: :[2]
end
end Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing member and collection routes.
Using wrong HTTP verbs.
✗ Incorrect
Custom member routes use 'member' and specify the HTTP verb like 'get'. Here, 'preview' is a member route accessed via GET.
5fill in blank
hardFill all three blanks to define nested routes for posts and comments with only 'create' and 'destroy' actions for comments.
Ruby on Rails
Rails.application.routes.draw do
resources :posts do
resources :comments, only: [:[1], :[2]], [3]: true
end
end Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Including unwanted actions in 'only'.
Forgetting to add 'shallow: true' for better route structure.
✗ Incorrect
The 'only' option limits actions to 'create' and 'destroy'. The 'shallow: true' option simplifies nested routes for comments.