2011-10-09 91 views
2

我有一個字段在它的汽車表叫做:vin(車輛識別號碼),我希望它使這個字段在寫東西時自動完成。好吧,你們都知道我的意思是。未定義的方法`自動完成'的軌道3和jquery

我在stackoverflow.com上找到了幾個答案,但沒有用。

我只是想說點..我在這裏跟着一個關於它的教程https://github.com/crowdint/rails3-jquery-autocomplete-app,它工作的很好,我只是做了一個新的應用程序。所以我決定把這個在我的應用程序,我的工作,我已經設置了一切都像它應該是,我想..但我得到這個錯誤

undefined method `autocomplete' for #< Class:0xb5e2da28> 

其次

app/models/car.rb:2 
app/controllers/cars_controller.rb:9:in `index' 

這裏是從幾行Car.rb

class Car < ActiveRecord::Base 
autocomplete :car, :vin 
    has_one :carname 
    has_one :carmodel 

一塊cars_controler的

def index 
    @search = Car.search(params[:search]) 
    @cars = @search.all.paginate :page => params[:page], :per_page => 18 
    # @cars = @search.relation.paginate :page => params[:page], :per_page => 18 

    respond_to do |format| 
     format.html # index.html.erb 
     format.xml { render :xml => @cars } 
     format.json { render :json => @cars.map(&:attributes) } 
    end 
    end 

鑑於我有

<%= form_tag do %> 
    <%= autocomplete_field_tag 'vin', '',car_autocomplete_car_name_path %> 
<% end %> 

在routes.rb中:get 'car/autocomplete_car_vin' 我做了耙路線和我的預期如果沒有自動完成至少一個工作形式。 請幫助使此工作。謝謝。

更新

感謝布里克我設法解決與未定義的方法的問題..形式正在顯示出來。但是,當我開始在字段中鍵入字母沒有自動完成顯示和甚至更多,在控制檯中我得到這個奇怪的事情:「ID」 =>「autocomplete_car_vin」

Started GET "/cars/autocomplete_car_vin?term=lh" for 127.0.0.1 at Sun Oct 09 07:50:06 +0300 2011 
    Processing by CarsController#show as JSON 
    Parameters: {"term"=>"lh", "id"=>"autocomplete_car_vin"} 
    SQL (0.6ms) SHOW TABLES 
    Car Load (0.2ms) SELECT `cars`.* FROM `cars` WHERE `cars`.`id` = 0 LIMIT 1 
Completed in 38ms 

ActiveRecord::RecordNotFound (Couldn't find Car with ID=autocomplete_vin): 
    app/controllers/cars_controller.rb:29:in `show' 

的PARAMATERS需要我鍵入的字母在這種情況下,lh(這些是我記錄中VIN號碼的前兩個字母),對於我寫的每個字母,我得到這個

「id」=>「autocomplete_vin」,找不到ID = autocomplete_vin的汽車

比較我的應用程序的控制檯結果,我將顯示教程中的控制檯操作,效果很好

Started GET "/welcome/autocomplete_brand_name?term=su" for 127.0.0.1 at Sun Oct 09 04:04:57 +0300 2011 
    Processing by WelcomeController#autocomplete_brand_name as JSON 
    Parameters: {"term"=>"su"} 
    Brand Load (0.6ms) SELECT `brands`.* FROM `brands` WHERE (LOWER(name) LIKE 'su%') ORDER BY name ASC LIMIT 10 
Completed 200 OK in 24ms (Views: 4.6ms | ActiveRecord: 0.6ms) 

因爲您看到沒有「id」=>「autocomplete_brand_name」。這裏有什麼問題,如果你們中的任何人都可以幫助我,請做..我真的需要做這樣的事情,所以我可以繼續前進。謝謝。

回答

9

autocomplete線會在你的控制器:

class CarsController < ApplicationController 
    autocomplete :car, :vin 
    # ... 
end 

此外,還要確保你已經綁定的寶石:

gem 'rails3-jquery-autocomplete` # in Gemfile 
$ bundle install # in console 

並重新啓動服務器。

+0

我做了捆綁abd寶石是在它的地方 關於自動完成..它應該在汽車模型作爲教程說反正 – rmagnum2002

+0

,我特里把它放在控制器,但沒有運氣..我已經完成的教程工作正常自動完成:車,:VIN在模型 – rmagnum2002

+1

您鏈接的教程明確指出要在控制器中放置'autocomplete'行:'在app/controllers/welcome_controller.rb文件的頂部添加這行:'。如果你看[這裏](https://github.com/crowdint/rails3-jquery-autocomplete-app/blob/master/app/controllers/welcome_controller.rb),該行在控制器中。事實上,[示例應用程序中的所有三個模型](https://github.com/crowdint/rails3-jquery-autocomplete-app/tree/master/app/models)都是完全空的。我的猜測是,您需要將'autocomplete'行移到控制器並重新啓動服務器。 – bricker

相關問題