0

我在本教程中做错了什么:https ://github.com/grosser/simple_auto_complete

顺便说一下,这是我的脚本:

首先我把它放在我的users_controller.rb

autocomplete_for :user, :username, :limit => 15, :order => 'created_at DESC'

在我的 routes.rb

namespace :profile do

  resources :users, :only => [:index] do 
    collection do
      post "search"
      get "autocomplete_for_user_name"
  end
end

在我的 application.js

//= require jquery
//= require jquery_ujs
//= require_tree .

//= require jquery.autocomplete.js

//= require jquery.js

jQuery(function($){//on document ready
    //autocomplete
    $('input.autocomplete').each(function(){
        var input = $(this);
        input.autocomplete(input.attr('data-autocomplete-url'),{
            matchContains:1,//also match inside of strings when caching
            //    mustMatch:1,//allow only values from the list
            //    selectFirst:1,//select the first item on tab/enter
            removeInitialValue:0//when first applying $.autocomplete
        });
    });
});

在我看来(app/views/profile/messages/compose.html.haml)

%div#page-info
  %span#title
    Compose
  %span#desc
    Compose a new Message

= form_for :message, :url => send_message_profile_messages_path do |message|
  %label{:for => "friend"} To:
  %br
  =message.text_field :auto_user_name , :class => 'autocomplete', 'data-autocomplete-url'=> autocomplete_for_user_name_profile_users_path

  %script{:type => "text/javascript"}jQuery(function($){//on document ready $('input.autocomplete').each(function(){var $input = $(this);$input.autocomplete($input.attr('data-autocomplete-url'));});});

  //= collection_select(:message, :friend_id, @friends, :id, :username)
  %br
  %label{:for => "message"} Body:
  %br
  = message.text_area :message
  %br
  = message.file_field :attachment
  %br
  = submit_tag "Send", :confirm => "Are you sure?"

我正在尝试使用用户名自动完成用户

注意:我还安装了 gem,我认为这没有问题。

我究竟做错了什么?

4

1 回答 1

0

鉴于//on document ready从中删除评论script,因为它都在单行中,我认为它正在评论包括 js 代码在内的整行。

或使用替代格式

:javascript
  //on document ready
  jQuery(function($){
    $('input.autocomplete').each(function(){
      var $input = $(this);
      $input.autocomplete($input.attr('data-autocomplete-url'));
    });
  });

添加//= require_self到应用程序文件以添加自己的 js 代码。

//= require jquery
//= require jquery_ujs
//= require_tree .
//= require jquery.autocomplete.js
//= require jquery.js 

jQuery(function($){//on document ready
    //autocomplete
    $('input.autocomplete').each(function(){
        var input = $(this);
        input.autocomplete(input.attr('data-autocomplete-url'),{
            matchContains:1,//also match inside of strings when caching
            //    mustMatch:1,//allow only values from the list
            //    selectFirst:1,//select the first item on tab/enter
            removeInitialValue:0//when first applying $.autocomplete
        });
    });
});

也尽量不要在require之间有空格。application.js 中的 rails 警告说

// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.

我一直在使用这个自动完成它对我很有用 https://github.com/crowdint/rails3-jquery-autocomplete

于 2012-08-14T09:07:52.867 回答