0

I have the following controller

class UsersController < ApplicationController
    def new
    end

    def create
#       render text: params[:user].inspect
        @user = User.new(user_params)
      @user.save
      redirect_to @user
  end

  def show
    @user = User.find(params[:id])
    respond_to do |format|
            format.html
            format.json {  render json: @user, success: true  }
        end
  end

    def index
        @users = User.all
    end

    def find_username
        @user = User.where(["username = ?", params[:username]])
        respond_to do |format|
            format.html
            format.json {  render json: @user, success: true  }
        end
    end

  private
      def user_params
        params.require(:user).permit(:username, :description)
      end
end

as you can see I have my own search method (find_username)> on my view I have the following ajax script:

$( document ).ready(function() {
    $('#user_username').blur(function() { 
        var value = $( this ).val();
        var url = "/username/" + value;     
    var data = {"username" : value}; 
    var dataType = "json"; 

    $.ajax({
      type: "GET",
      url: url,
      data: data,
      success: function(response){
        alert("EXISTS");
      },
      error: function(response){
        alert("NO EXISTS");
      },
      dataType: dataType
    }); 
    });
});

But I always get "EXISTE" No matter if the record don't exist on the db. I don't understand why. Here is my rails routes:

Testing::Application.routes.draw do

  resources :users
    get 'username/:username', to: "users#find_username"

  root 'welcome#index'

end

Thanks in advance for your help!

Jean
  • 5,201
  • 11
  • 51
  • 87
  • are you returning 200 header each time ? Ajax is recognizing success / error by header you send back. – Damian Krawczyk Feb 03 '14 at 16:47
  • Damian, how can I return error o 404??? – Jean Feb 03 '14 at 16:49
  • 2
    You need to send header("HTTP/1.0 404 Not Found") I', mot RoR developer but this might be helpful -> http://stackoverflow.com/questions/2385799/how-to-redirect-to-a-404-in-rails – Damian Krawczyk Feb 03 '14 at 16:54
  • you should put a success fail response in your response data. then check against that in your success message, the error callback is used for errors like parse and 404 errors etc. As if you just put out a 404 header your program will not know what actually is the error. – Patrick Evans Feb 03 '14 at 16:54

1 Answers1

0

The problem is that you are always returning a 200 success status code no matter if you find the user or not. If you need to handle the response with status codes one way to go about it would be to change your user search code with something like this:

@user = User.where(["username = ?", params[:username]]).first!

This will raise an ActiveRecord::RecordNotFound error so you can get a 404 in your client side.

As a side note, the where statement you have will always return an array of users matching the search criteria (even if the user is only one). So you should put .first in the end to get the first user that is found. The bang method just raises the exception you need and it works like find.

Nikos
  • 1,052
  • 5
  • 7