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ます。すべてが1行になっているため、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のレール警告は言う

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

私はこのオートコンプリートを使用してい ます

于 2012-08-14T09:07:52.867 に答える