开发者

Autofill using jquery and RoR error?

开发者 https://www.devze.com 2023-03-08 08:20 出处:网络
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:

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: func开发者_运维问答tion( 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.


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


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' }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号