Mo.
Mo.

Reputation: 42503

Autofill using jquery and RoR error?

Im trying to autofill a text filed using jquery plugin Autocomplete but im running in to a couple of problems, no list is genrated in the view and i get an error in firbug saying:

500 Internal Server Error, <h1>Template is missing</h1>
<p>Missing template messages/query with {:handlers=&gt;[:erb, :builder, :rjs, :rhtml, :rxml], :locale=&gt;[:en, :en], :formats=&gt;[:json, :js, &quot;*/*&quot;]} in view paths &quot;/Users/aldeirm2/Desktop/CMS2/app/views&quot;</p>. 

im guessing it has to do with the way im trying to return the json object?

Here is javascript code:

$(function() {
    var cache = {},
        lastXhr;
    $( "#username" ).autocomplete({
        minLength: 1,
        source: function( request, response ) {
            var term = request.term;
            if ( term in cache ) {
                response( cache[ term ] );
                return;
            }

            lastXhr = $.getJSON( "/usernames", request, function( data, status, xhr ) {
                cache[ term ] = data;
                if ( xhr === lastXhr ) {
                    response( data );
                }
            });
        }
    });
});

HTML code:

<label for="username">To:</label>  <input id="username" /><br />

search method in controller (routed to used "/search"):

  def query
    @users = User.auto_fill(params[:term])

    logger.debug "This is the USERS" + @users.first.username
    return @users.collect{|x| {:label => x.name, :id => x.id}}.to_json
  end

auto_fill method in model:

def self.auto_fill(search)
    query = "%#{search}%"
    where("username like ?", query)
  end

any help would be great thanks.

Upvotes: 1

Views: 247

Answers (2)

Dty
Dty

Reputation: 12273

The error tells you exactly what's wrong. You're missing a template for messages/query. You need to fix that first. In your controller/action do something like this instead

render :json => { :success => 'your info here' }

Upvotes: 1

pthurlow
pthurlow

Reputation: 1111

i think because you are not rendering anything, rails is trying to find a query template to render. try:

render :json => @users.collect{|x| {:label => x.name, :id => x.id}}

or something along those lines

Upvotes: 2

Related Questions