2012-08-14 98 views
0

我是怎么在这个教程做的不对:https://github.com/grosser/simple_auto_complete自动完成Rails的它不工作

这里是我的脚本的方式:

首先,我把这个在我的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 
     }); 
    }); 
}); 

在我的意见(应用程序/视图/资料/消息/ 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?" 

我想用自己的用户名自动填充用户(S )

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

我在做什么错了?

+1

你得到的萤火控制台任何JavaScript错误的伟大工程? – PradeepKumar 2012-08-14 08:11:19

+0

你有没有加过javascript的叫 – PriteshJ 2012-08-14 08:26:16

+0

你叫什么意思javascript @PriteshJ – user1559755 2012-08-14 08:35:39

回答

0

在查看 从script中删除评论//on document ready,因为它都在单行中我认为它是评论包括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 
     }); 
    }); 
}); 

也请尽量不要在要求之间留有空格。 轨道中的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. 

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

+0

无论如何感谢。但我没有明白 – user1559755 2012-08-14 09:53:58

+0

你可以继续前进,并询问更多信息,直到你做对了 – PriteshJ 2012-08-14 11:30:43