Performance: Route parameters
Route parameters affect server-side routing speed and can influence initial page load time by determining which controller action runs.
resources :users, only: [:show] do
member do
get 'profile'
get 'settings'
end
endget '/users/:id', to: 'users#show' get '/users/:id/profile', to: 'users#profile' get '/users/:id/settings', to: 'users#settings'
| Pattern | Routing Checks | Server Response Time | Impact on LCP | Verdict |
|---|---|---|---|---|
| Multiple similar parameter routes | Multiple pattern matches per request | Higher latency under load | Delays LCP | [X] Bad |
| Resourceful routing with parameters | Single grouped pattern match | Lower latency | Improves LCP | [OK] Good |
get '/users/:id', to: 'users#show'id means this part of the URL is a variable, called a route parameter.post_id in Rails?:post_id.get '/products/:id', to: 'products#show'def show @product_id = params[:id] end
@product_id be if the URL is /products/42?:id in the route captures the URL part '42' as a string in params[:id].@product_id = params[:id], so @product_id is the string '42'. Rails does not convert it automatically to integer.get '/orders/:order_id/items/:item_id', to: 'orders#item'
def item @order = Order.find(params[:id]) @item = Item.find(params[:item_id]) end
:order_id and :item_id, so params keys are :order_id and :item_id.params[:id] which does not exist. It should use params[:order_id] to find the order.post_id and id for the comment?post_id comes before id for comment./posts/:post_id/comments/:id capturing post's ID and comment's ID in order.Pass the test and this pattern moves unknown