how to differentiate get and post when it has same name in rails

# In your routes.rb file
resources :items do
  collection do
    get 'search' # Route for GET request
    post 'search' # Route for POST request

# In your controller
class ItemsController < ApplicationController
  def search
      # Logic for handling POST request
      # Logic for handling GET request


  1. Routes Definition: In routes.rb, using resources :items, you create RESTful routes for an 'items' resource. Within that resource block, the collection method allows you to define additional routes that don't require an id.

  2. GET and POST Routes: Both get 'search' and post 'search' are defined within the collection block. They have the same URL ('/items/search') but differ in their HTTP methods (GET and POST).

  3. Controller Action: Inside the ItemsController, the search action handles requests to '/items/search'. Within this action, is used to check if the request is a POST request. Depending on the request type (GET or POST), the logic for each type of request can be executed separately within the conditional blocks.