-1

I'm working on the autocomplete feature in my Rails app and following the answer from How to set-up jquery-ui autocomplete in Rails. When I type something into input, this error shows up: ActiveRecord::RecordNotFound (Couldn't find User with 'id'=json)? (:term is still equal to whatever I type) Why is there id=json?

<div>
  <input id="select_user" />
  <input id="link_user_id" name="link[user_id]" type="hidden"/>
</div>

<script type="text/javascript">
  $(function() {
    $('#select_user').autocomplete({
      minLength: 1,
      source: '<%= user_path(:json) %>',
    })
    .data("ui-autocomplete")._renderItem = function( ul, item ) {
      return $( "<li></li>" )
        .data( "ui-autocomplete-item", item )
        .append( "<a>" + item.user.name + "</a>" )
        .appendTo( ul );
    };
  });
</script>

User controller:

def index
  if params[:term]
    @users = User.find(:all, :conditions => ['name LIKE ?', "%#{params[:term]}%"])
  end

  respond_to do |format|
    format.html
    format.json { render :json => @users.to_json }
  end
end
Vbp
  • 1,912
  • 1
  • 22
  • 30
j doe
  • 31
  • 3
  • what version of rails are you using? User.find(:all) is not supported in newer version of rails try `User.where()` – Vbp Mar 10 '19 at 18:57
  • @Vbp, it's 5.0.1 – j doe Mar 10 '19 at 19:00
  • try something like this `User.where("name LIKE ?", "%#{params[:term]}%")` – Vbp Mar 10 '19 at 19:01
  • just tried, but getting the same error `Couldn't find User with 'id'=json`. I was wondering if it has to do with this line 'source: '<%= user_path(:json) %>'` – j doe Mar 10 '19 at 19:04
  • that looks like a different error its saying couldn't find Font instead of User ? – Vbp Mar 10 '19 at 19:06
  • try adding `byebug` in index action and check the value of params[:term] in console.. you should also be able to see the stack trace and the query that rails is sending to database in console – Vbp Mar 10 '19 at 19:07
  • @Vbp, ahh sorry, should be `User` – j doe Mar 10 '19 at 19:08
  • `params[:term]` is populated by whatever i type in `input`, I'm just confused about why it's complaining about `id`. I don't seem to pass any `id` back to controller – j doe Mar 10 '19 at 19:10
  • the first argument to `find()` is `id` so it seems like its trying to use the `find()` method? Did you changed it to `where()` as I suggested above? may be try restarting server – Vbp Mar 10 '19 at 19:13
  • @Vbp, yeah, im using `.where` but getting the same error `ActiveRecord::RecordNotFound (Couldn't find User with 'id'=json)` – j doe Mar 10 '19 at 19:16
  • can you add stack trace from console? I am not sure where is `find()` getting called from even after replacing that call with `where()` which shouldn't raise any exception as it just returns empty array if no matching records are found – Vbp Mar 10 '19 at 19:19

2 Answers2

4

You issue is here:

source: '<%= user_path(:json) %>'

The first attribute that is passed into these url helpers is always the object or the id. So your issue is way before everything that was mentioned in the comments.

user_path({}, format: :json) would it be for adding format. This snipped will prob return ActiveRecord::RecordNotFound (Couldn't find User with 'id'=nil). So you have to find a way of passing id in.

I guess you want all to have all users returned that match in you index action. For this you have to use users_path(format: :json).

Denny Mueller
  • 3,505
  • 5
  • 36
  • 67
0

Try newer syntax

User.where("name LIKE ?", "%#{params[:term]}%")

I think the syntax you are using is deprecated in rails 3

Vbp
  • 1,912
  • 1
  • 22
  • 30