2

我正在关注本教程 [https://github.com/crowdint/rails3-jquery-autocomplete][1],但我遇到了问题。

我的模型:

class Category < ActiveRecord::Base
  attr_accessible :name

  has_many :products
end

class Product < ActiveRecord::Base
  attr_accessible :name, :category_id

  belongs_to :category
end

架构:

  create_table "categories", :force => true do |t|
    t.string   "name"
  end

  create_table "products", :force => true do |t|
    t.string   "name"
    t.integer  "category_id"
  end

这是我的控制器:

autocomplete :category, :name

我的布局是这样的:

<%= javascript_include_tag :defaults, "autocomplete-rails.js"  %>

我的路线有这个:

  resources :products do
    get :autocomplete_category_name, :on => :collection
  end

我的表格有这个:

<%= f.autocomplete_field :category_name, autocomplete_category_name_products_path %>

我的 app/assets/javascript 文件夹中有 autocomplete-rails.js。但是由于某种原因,我不断收到此错误:

undefined method `category_name'
4

2 回答 2

1

你应该使用

<%= f.autocomplete_field :category, autocomplete_category_name_products_path, :id_element => '#product_category_id' %>
<%= f.hidden_field :category_id %>
于 2013-02-27T23:26:53.117 回答
0

确保您已安装并捆绑了 gem,然后再次重新启动服务器

gem 'rails3-jquery-autocomplete`
于 2012-10-16T19:50:34.420 回答