title: How to display Ajax error message in rails?
body: <p><strong>script</strong></p>
  
  <pre><code> &lt;script type="text/javascript"&gt;
    $('.v_name').each(function() {
      $(this).on('ajax:success', function(event, data, status, xhr) {
        var dat, id ;
        id = $(this).attr('id');
       dat = JSON.parse(data);
        $('#' + id).html(dat.name.toUpperCase());
      });
  
    });
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>index.html.erb</strong></p>
  
  <pre><code> &lt;% @vehicles.each do |vehicle| %&gt;
  &lt;tr&gt;
      &lt;td&gt;&lt;%= best_in_place vehicle,:name, class: "v_name", id: vehicle.id%&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= link_to '&lt;i class="fa fa-trash-o"&gt;&lt;/i&gt;'.html_safe, vehicle, method: :delete,  remote: true, data: { confirm: "Are you sure to delete &lt;b&gt;\"#{vehicle.name}\"?&lt;/b&gt;", commit: "OK" }, title: "Delete Vehicle", class: "btn btn-danger delete_vehicle" %&gt;
  
      &lt;/td&gt;
  &lt;/tr&gt;       
  &lt;%end%&gt;
  </code></pre>
  
  <p><strong>vehicles_controller.rb</strong></p>
  
  <pre><code>    class VehiclesController &lt; ApplicationController
    before_action :set_vehicle, only: [:show, :edit, :update, :destroy]
    load_and_authorize_resource
  
     # skip_before_action :verify_authenticity_token
  
    # GET /vehicles
    # GET /vehicles.json
    def index
      @q = Vehicle.search(params[:q])
      @vehicles = @q.result(:distinct =&gt; true).order_by([:updated_at, :desc]).page(params[:page]).per(5)
      @vehicle = Vehicle.new
    end
  
    # GET /vehicles/1
    # GET /vehicles/1.json
    def show
    end
  
    # GET /vehicles/new
    def new
  
    end
  
    # GET /vehicles/1/edit
    def edit
    end
  
    # POST /vehicles
    # POST /vehicles.json
    def create
      params[:vehicle][:name] = params[:vehicle][:name].upcase if !params[:vehicle][:name].nil?
      @vehicle = Vehicle.new(vehicle_params)
      @vehicles = Vehicle.all.order_by([:updated_at, :desc]).page(params[:page]).per(5)
      respond_to do |format|
        if @vehicle.save
          format.html { redirect_to :back, notice: 'Vehicle was successfully created.' }
          format.json { render action: 'index', status: :created, location: @vehicle }
          format.js
        else
          format.js
          format.html { render action: 'new' }
          format.json { render json: @vehicle.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # PATCH/PUT /vehicles/1
    # PATCH/PUT /vehicles/1.json
    def update
          params[:vehicle][:name] = params[:vehicle][:name].upcase if !params[:vehicle][:name].nil?
  
      respond_to do |format|
  
        if @vehicle.update(vehicle_params)
          format.html { redirect_to @vehicle, notice: 'Vehicle was successfully updated.' }
          format.json {render json: @vehicle, status: :ok}
        else
          format.html { render action: 'edit' }
          format.json { render json: @vehicle.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # DELETE /vehicles/1
    # DELETE /vehicles/1.json
    def destroy
      @vehicle.destroy
      respond_to do |format|
        format.html { redirect_to vehicles_url, notice: "#{@vehicle.name} deleted successfully" }
        format.json { head :no_content }
        format.js { render :layout =&gt; false}
      end
    end
  
    def vehicle_search
  
      @q = Vehicle.search(params[:q])
      @vehicles = @q.result(:distinct  =&gt; true).order_by([:updated_at, :desc]).page(params[:page]).per(5)
  
      respond_to do |format|
        format.html
        format.js
      end
    end
    private
      # Use callbacks to share common setup or constraints between actions.
      def set_vehicle
        @vehicle = Vehicle.find(params[:id])
      end
  
      # Never trust parameters from the scary internet, only allow the white list through.
      def vehicle_params
        params.require(:vehicle).permit(:name, :created_at, :updated_at)
      end
  end
  </code></pre>
  
  <p><strong>vehicle.rb</strong></p>
  
  <pre><code> class Vehicle
    include Mongoid::Document
      include Mongoid::Timestamps
  
    field :name, type: String
    validates :name, presence: true, uniqueness: true, :format =&gt; {:with =&gt; /[1-9a-zA-Z][0-9a-zA-Z ]{3,}/}
    has_many :pre_processings
    has_many :batch_counts
  
  
  end
  </code></pre>
  
  <p>How to display Ajax error message in rails?</p>
  
  <p>Here I am update name to uppercase using above script. If there is duplicate names it's show error message through Ajax how's it possible. </p>
  
  <p>Advance Thanks.</p>