Ruby on Rails - member and collection routes in ruby on rails - ruby on rails tutorial - rails guides - rails tutorial - ruby rails
Defining a member block inside a resource creates a route that can act on an individual member of that resource-based route
esources :posts do
member do
get 'preview'
end
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
This generates the given below member route:
get '/posts/:id/preview', to: 'posts#preview'
# preview_post_path
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
Collection routes allow for creating routes that can act on a collection of resource objects
resources :posts do
collection do
get 'search'
end
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
This generates the giben below collection route
get '/posts/search', to: 'posts#search'
# search_posts_path
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
Another syntax:
resources :posts do
get 'preview', on: :member
get 'search', on: :collection
end