问题
I am looking for a good solution for a probably typical problem of managing models with HABTM association in Rails.
Let's assume that we have two models -- products and categories:
Products
has_many :categorizations
has_many :categories, :through => :categorizations
Categories
has_many :categorizations
has_many :products, :through => :categorizations
Categorization
belongs_to :product
belongs_to :category
Pat Shaughnessy is developing modified auto_complete plugin which can allow to manage one-to-many (and many-to-many) association:
alt text http://img28.imageshack.us/img28/9141/samplef.th.png alt text http://img406.imageshack.us/img406/4075/sampleautocomplete.th.png
For someone that would be enough, but I don't really like so many text fields. I guess it is possible to combine the Ryan Bates' screencasts used by Pat in a slightly different way, namely, using one text field with autocomplete:
alt text http://img405.imageshack.us/img405/9576/betterautocomplete.th.png
Also it would be better to remove from the list of suggested tasks (one-to-many), or products|categories (many-to-many) those which have been already added.
In the case of products-categories relationship, such auto completion would be used both in the form for product, and in the form for category.
I hope that there is an existing plugin for that. I am not very experienced in Ruby, and don't really know how to implement such logic by myself.
Any help is appreciated!
回答1:
The problem is nicely solved using jQuery Tokeninput plugin, as shown by Ryan Bates: http://railscasts.com/episodes/258-token-fields
回答2:
I also need a similar. but so far I found two plugins for jQuery.
The "Tagger" chrisiufer.com/tagger, but without autocomplete function.
And "Advanced Uses of Auto Complete" http://education.llnl.gov/jquery/adv_auto.lasso
It would be cool if someone could remake it for the Ruby on Rails. :) If you have a ready solution, please put the code!
回答3:
Maybe the model_auto_completer will help.
http://agilewebdevelopment.com/plugins/model_auto_completer
来源:https://stackoverflow.com/questions/1510935/how-to-do-habtm-management-with-auto-completion-in-rails