Performance: RESTful resource routes
This affects the server response time and client-side navigation speed by structuring URL routes efficiently.
resources :posts
get '/posts/new', to: 'posts#new' post '/posts', to: 'posts#create' get '/posts/:id/edit', to: 'posts#edit' patch '/posts/:id', to: 'posts#update' delete '/posts/:id', to: 'posts#destroy'
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual route definitions for each action | N/A | N/A | N/A | [!] OK |
| RESTful resource routes with 'resources :posts' | N/A | N/A | N/A | [OK] Good |
resources :books line do in a Rails routes file?resources methodresources method in Rails automatically generates all seven standard RESTful routes (index, show, new, create, edit, update, destroy) for a given resource.resources :booksresources :books creates all these routes for the books controller, enabling standard CRUD actions.resources :books = all RESTful routes [OK]index and show routes for a resource named articles?resources with only optiononly: [:index, :show] with resources.only. resources :articles, except: [:index, :show] uses except which excludes actions, not includes. resource :articles, only: [:index, :show] uses singular resource which is for single objects, not collections. resources :articles, only: index, show syntax is invalid because only expects an array.only and array [OK]resources :users, except: [:destroy], which HTTP verb and path will NOT be available?except: [:destroy]except option removes the specified action routes. Here, destroy is excluded, so no route for deleting a user is created./users/:id. This route will be missing.resources :products, only: :show, :edit
only optiononly option expects an array of symbols like only: [:show, :edit]. Writing only: :show, :edit passes :show as the only option and :edit is outside the hash, causing syntax error.resources supports only. The resource name should be plural for collections. Listing all actions is optional.only option should be an array of symbols, not separate symbols. -> Option Donly needs array syntax [OK]orders but only allow index, show, and create actions. Additionally, you want a custom member route cancel that responds to POST requests. Which routes file code is correct?only: [:index, :show, :create] to restrict routes to these actions.post 'cancel', on: :member to add a POST route for cancel on individual orders.resource which is wrong for collections.Pass the test and this pattern moves unknown