自动完成Rails它无法正常工作

时间:2012-08-14 05:54:24

标签: ruby-on-rails ruby autocomplete

本教程中我做错了什么: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?"

我正在尝试使用用户名自动填充用户

注意:我也安装了宝石,我认为没有问题。

我做错了什么?

1 个答案:

答案 0 :(得分: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