1

私はAuto-complete AssociationsのRailscastに従ったが、私はオートコンプリートの半分で立ち往生している。私はPrototypeではなくJqueryを代わりに使用しているので、オートコンプリートの半分をどうやって動かすか分からない。どうしたの?上記別のモデルからどのように自動完成しますか?

def location_name 
    location.business_name if location 
end 

def location_name=(business_name) 
    self.location = Location.find_by_business_name(business_name) unless business_name.blank? 
end 

私は私の<%= f.text_field :location_name %>ためlocation_nameに私の場所のモデル:business_nameを定義した仮想属性を行います

製品モデル:

これはRailscastが持っているものに比べてカスタマイズされ製品がロケーションに属しているため、製品が形成されます。

編集:これはJquery Autocomplete Gemを使用していましたが、私は選択肢がありません。

私の製品:nameは魅力的です。

<%= f.autocomplete_field :name, autocomplete_product_name_products_path %> 

しかし、私は:location_nameのためにこのような何かを置けばその良いがありません。

<%= f.autocomplete_field :location_name, autocomplete_product_location_name_products_path %> 

答えて

3

それは、簡単だったattr_accessorを追加する必要がありました、これは他のプロジェクトからの私の完成コードです:

<%= form_for(@business_address) do |f| %> 
    <%= f.error_messages %> 
    <%= f.label :name, "Address" %><br /> 
    <%= f.autocomplete_field :name, autocomplete_business_address_name_business_addresses_path %> 
    <%= f.label :business_name, "Business" %> 
    <%= f.autocomplete_field :business_name, autocomplete_business_name_business_addresses_path %> 
    <%= f.submit %> 
<% end % 


class BusinessAddress < ActiveRecord::Base 
    attr_accessible :name, :business_name 
    belongs_to :business 
    attr_accessor :business_name 


    def business_name 
     business.name if business 
    end 

    def business_name=(name) 
     self.business = Business.find_or_create_by_name(name) unless name.blank? 
    end 
end 

class BusinessAddressesController 
    autocomplete :business, :name, :full => true 
    autocomplete :business_address, :name, :full => true 
end 

routes.rbを

resources :business_addresses do 
     get :autocomplete_business_name, :on => :collection 
     get :autocomplete_business_address_name, :on => :collection 
end 

、あなたのアプリケーションのレイアウトに必要なファイルを指定してくださいまたは何らかの奇妙な理由で動作しません(レール3.0.9)

<%= javascript_include_tag "autocomplete-rails", "jquery-ui-1.8.16.custom.min" %> 
1

私はわかりませんが、あなたはautocomplete_location_location_name_products_pathを使用してみましたが?

0

Jquery-Autocompleteのオートコンプリートを取得できなかったので、自分の場所にJquery-TokenInputプラグインを使用し始めたばかりで、Limit to 1トークンのみを設定したので、Railscastsから多くを学びました。

関連する問題