0
0
Ruby on Railsframework~30 mins

Member and collection routes in Ruby on Rails - Mini Project: Build & Apply

Choose your learning style9 modes available
Rails Member and Collection Routes
📖 Scenario: You are building a simple blog application in Rails. You want to add special routes to manage posts. Some routes will act on individual posts (member routes), and some will act on the whole collection of posts (collection routes).
🎯 Goal: Create member and collection routes inside the resources :posts block in config/routes.rb. Add a member route called preview that shows a preview of a single post. Add a collection route called search that allows searching all posts.
📋 What You'll Learn
Create a resources :posts block in config/routes.rb
Inside the block, add a member route get 'preview'
Inside the block, add a collection route get 'search'
💡 Why This Matters
🌍 Real World
Member and collection routes help you add custom actions in Rails apps that work on single items or entire collections, like previewing a post or searching all posts.
💼 Career
Understanding how to customize routes with member and collection blocks is essential for Rails developers to build flexible and maintainable web applications.
Progress0 / 4 steps
1
Create the basic posts resource route
In config/routes.rb, create a resources :posts block to define standard RESTful routes for posts.
Ruby on Rails
Need a hint?

Use resources :posts inside the Rails.application.routes.draw do ... end block.

2
Add a member route for preview
Inside the resources :posts block, add a member route get 'preview' to allow previewing a single post.
Ruby on Rails
Need a hint?

Use member do block inside resources :posts and add get 'preview' inside it.

3
Add a collection route for search
Inside the resources :posts block, add a collection route get 'search' to allow searching all posts.
Ruby on Rails
Need a hint?

Use collection do block inside resources :posts and add get 'search' inside it.

4
Complete the routes with both member and collection routes
Ensure the resources :posts block contains both the member route get 'preview' and the collection route get 'search' inside their respective blocks.
Ruby on Rails
Need a hint?

Check that both member do and collection do blocks are inside resources :posts with their respective get routes.