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