开发者

undefined method `autocomplete' rails 3 and jquery

I have the car tables with a field in it called: vin (Vehicle Identification Number), I want it to make this field to autocomplete when writing something.. well, you all know what I mean.

I found a few answers on stackoverflow.com but no use.

I'll just get to the point.. I followed a tutorial about it here https://github.com/crowdint/rails3-jquer开发者_Python百科y-autocomplete-app and it works great, I just did it a new application. So I decided to put this in my app that I work on, I've set up everything like it should be, I guess.. but I get this error

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

followed by

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

here is a few lines from Car.rb

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

a piece of 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

in view I have

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

in routes.rb: get 'car/autocomplete_car_vin' I did the rake routes and I expected if not an autocomplete at least a working form. Please help to make this working. Thanks.

Update

Thanks to bricker I managed to solve the problem with undefined method.. the form is showing up now. But when I start typing letters in the field there is no autocomplete showing and even more, in the console I get this weird thing: "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'

The paramaters takes the letters that I type in this case lh (these are first 2 letters from a VIN number in my records) and for each letter I write I get this

"id"=>"autocomplete_vin" and Couldn't find Car with ID=autocomplete_vin

to compare my console results from my app, I'll show the console action from tutorial that works great

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)

as you see there is no "id"=>"autocomplete_brand_name". what is the problem here if any of you can help me, please do.. I really need to do thid so I can move on. Thanks.


The autocomplete line goes in your controller:

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

Also, make sure you've bundled the gem:

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

and restart the server.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜