rails和jquery移动自动完成上的406(Not Acceptable)错误

时间:2011-09-25 03:34:08

标签: jquery ruby-on-rails jquery-ui autocomplete jquery-mobile

当我输入自动填充字段时,我一直收到此406(不可接受)错误。

我正在使用jquery mobile和jquery ui autocomplete和rails

这是我的jquery

$("#request_artist").focus(function(){ 
    $("#request_artist").autocomplete({ 
        source: function(req, add){ 
            $.getJSON(artistRequestUrl, req, function(data) { 
                var suggestions = data.suggestions; 
                add(suggestions);    
            });   
        },
        change: function() {
            $("#request_song").attr("disabled", false); 
            $("#request_submit").attr("disabled", false); 
            $("#request_like_reason").attr("disabled", false); 
            $('.disable_color').css("color", "black");   
        },    
    });
});    

这是我的HTML

<div data-role="fieldcontain">
    <label for="search">Artist</label>
    <input type="search" name="password" id="request_artist" value="" />
</div>

这是我的rails动作

def ajax_artist
  bands = Band.all
  artists = bands.map(&:name).uniq.sort
  filter_input(artists, params[:term])
  Rails.logger.info @all_instances_hash.inspect
  respond_to do |format|
    format.json { render :json => @all_instances_hash}
  end
end

如果我在控制台中查看记录器,如果我输入字母c,我会得到@all_instances_hash

 {:query=>"c", :suggestions=>["Casting Crowns", "John Mark McMillan"]}

但自动完成功能不起作用......任何想法

1 个答案:

答案 0 :(得分:2)

我猜Rails认为你要回HTML。你的ajax_artist只能用JSON做出明智的回应,所以试着完全忽略这个格式:

def ajax_artist
  bands = Band.all
  artists = bands.map(&:name).uniq.sort
  filter_input(artists, params[:term])
  Rails.logger.info @all_instances_hash.inspect
  render :json => @all_instances_hash
end