2012-07-04 32 views
2

我正在努力寻找我对rails3-jquery-autocomplete gem有问题的答案。rails3-jquery-autocomplete将参数传递给作用域查询

我有2个型号。环境&物业。

class Environment < ActiveRecord::Base 
    has_many :properties 

name: string, envfile: string 

class Property < ActiveRecord::Base 
    scope :with_environment_id, where(:environment_id => 14) 
    belongs_to :environment 

name: string, value: string, environment_id: integer 

我有显示属于一个特定的环境和该视图中的下面的代码,其自动搜索基于所述属性名称

<%= form_tag('tbd')%> 
    <%= autocomplete_field_tag :property_name, '', autocomplete_property_name_properties_path> 

属性控制器的所有属性的图

class PropertiesController < ApplicationController 
    autocomplete :property, :name, :scopes => [:with_environment_id] 

and routes.rb has

get :autocomplete_property_name, :on => :collection 

自动完成工作正常,但返回返回属性表中的所有记录。我只希望在视图中显示属于环境的属性。

正如你所看到的,我已经在属性模型中定义了一个范围,我在其中硬编码了一个有效的environment_id,使其按照我的意愿工作。

所以我的问题是我如何从视图/控制器传回environment_id回范围?

也许我从错误的角度来解决这个问题。

道歉,如果它是一个愚蠢的问题,但它一直困扰着我整天。

回答

0
def get_autocomplete_items(parameters) 
    super(parameters).where(:user_id => current_user.id) 
end