0
0
Ruby on Railsframework~20 mins

RESTful resource routes in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
RESTful Routes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the URL and HTTP method for the 'edit' action in a RESTful resource route?
Given a resource named articles, what is the correct URL path and HTTP method to access the edit action for the article with ID 5?
APOST /articles/5/edit
BGET /articles/5/edit
CGET /articles/edit/5
DPATCH /articles/5/edit
Attempts:
2 left
💡 Hint
Remember, the edit action shows a form to change an existing resource and uses GET.
📝 Syntax
intermediate
2:00remaining
Which route declaration creates all RESTful routes for a resource named 'products'?
In Rails routing, which line correctly generates all seven standard RESTful routes for the products resource?
Amatch 'products', via: :all
Bresource :products
Cget 'products'
Dresources :products
Attempts:
2 left
💡 Hint
Plural form is important for multiple resources.
state_output
advanced
2:00remaining
What controller action and params are triggered by this route?
Given the route resources :comments, what controller action and params hash will be called when a DELETE request is sent to /comments/42?
AAction: destroy, Params: { id: '42' }
BAction: delete, Params: { comment_id: '42' }
CAction: destroy, Params: { comment_id: '42' }
DAction: remove, Params: { id: '42' }
Attempts:
2 left
💡 Hint
The DELETE method on a resource with ID calls the destroy action with id param.
🔧 Debug
advanced
2:00remaining
Why does this custom route conflict with RESTful routes?
Consider these routes in routes.rb:
resources :users
get '/users/new', to: 'users#show'
What problem will this cause when accessing /users/new?
AThe custom route overrides the RESTful 'new' action, causing the wrong controller action to run.
BThe RESTful 'show' action will never be called for any user.
CIt causes a syntax error in the routes file.
DNo problem; both routes work fine together.
Attempts:
2 left
💡 Hint
Routes are matched top to bottom; custom routes can override RESTful ones.
🧠 Conceptual
expert
3:00remaining
How does Rails handle nested RESTful resource routes in URL and params?
Given these nested routes:
resources :authors do
  resources :books
end
What is the URL and params hash for accessing the 'show' action of book with ID 7 belonging to author with ID 3?
AURL: /authors/3/books/7, Params: { id: '3', book_id: '7' }
BURL: /books/7/authors/3, Params: { id: '7', author_id: '3' }
CURL: /authors/3/books/7, Params: { author_id: '3', id: '7' }
DURL: /books/7, Params: { author_id: '3', id: '7' }
Attempts:
2 left
💡 Hint
Nested resources include parent resource ID in URL and params with key named after parent resource.