Api Crud and search api and rspec

 routes.rb


Rails.application.routes.draw do

  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html

  scope :api do

    # resources :users, :defaults => { :format => 'json' }

      get '/users' => 'users#index', :defaults => { :format => 'json' }

      get '/user/:id' => 'users#show', :defaults => { :format => 'json' }

      post '/user' => 'users#create', :defaults => { :format => 'json' }

      put '/user/:id' => 'users#update', :defaults => { :format => 'json' }

      delete '/user/:id' => 'users#destroy', :defaults => { :format => 'json' }

      get '/typeahead/:input' => 'users#search', :defaults => { :format => 'json' }


  end

end

==========================================================================

add ApplicationController.rb


class ApplicationController < ActionController::Base
skip_before_action :verify_authenticity_token

  respond_to :json

end
==========================================================================
add controlelr 

rails g scaffold User first_name:string last_name:string email:string

class UsersController < ApplicationController
  before_action :set_user, only: %i[ show edit update destroy ]

  # GET /users or /users.json
  def index
    @users = User.all
  end

  # GET /users/1 or /users/1.json
  def show
  end

  # GET /users/new
  def new
    @user = User.new
  end

  # GET /users/1/edit
  def edit
  end

  # POST /users or /users.json
  def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        format.html { redirect_to user_url(@user), notice: "User was successfully created." }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /users/1 or /users/1.json
  def update
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to user_url(@user), notice: "User was successfully updated." }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit, status: :unprocessable_entity }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /users/1 or /users/1.json
  def destroy
    @user.destroy

    respond_to do |format|
      format.html { redirect_to users_url, notice: "User was successfully destroyed." }
      format.json { head :no_content }
    end
  end

  def search
     key = params[:input]
    @users = User.where('first_name LIKE  ? OR last_name LIKE ? OR email LIKE ?', "%#{key}%", "%#{key}%", "%#{key}%")   
     render 'index'
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_user
      @user = User.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def user_params
      params.permit(:first_name, :last_name, :email)
    end
end
==========================================================================
seed .rb

# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
#   movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
#   Character.create(name: 'Luke', movie: movies.first)

User.create(first_name: 'albert', last_name: 'einstein', email: 'ae@relativity.com' )
User.create(first_name: 'marie', last_name: 'curie', email: 'mc@radiation.com' )
User.create(first_name: 'issac', last_name: 'newton', email: 'in@gravity.com' )
User.create(first_name: 'galileo', last_name: 'galilei', email: 'gg@astronomy.com' )


==========================================================================



Comments

Popular posts from this blog

rvm setup new ruby install system

masking account number

Ruby install for new system