#102 Auto-Complete Association
Usually a select menu is used for setting a belongs_to association, but in this episode I will show you how to use a text field with auto completion.
- Download:
- mp4Full Size H.264 Video (19.4 MB)
- m4vSmaller H.264 Video (13.3 MB)
- webmFull Size VP8 Video (34.5 MB)
- ogvFull Size Theora Video (27.9 MB)
Thanks very much - awesome!
Thaks! :P
Hi
Your screencast make programming rails even more easy !
But there is something strange in the convention used for returning the matching categories.
"categories/index.js.erb" assume you returning javascript code ?
How does it works if I need to get real js elsewhere in my project ?
Thanks
Mourad Hammiche: instead of use formatted_categories_path(:js) you can use categories_path and at the :with clause you specify something like "partial=true" or "autocomplete=true", then you handle it in your controller to render a special HTML template for autocomplete. Imao it's better then use the js formatted being not semantic.
@Mourad, great questions, I was wondering if someone would bring this up.
It would be really nice if the auto_complete plugin supported returning javascript (with rjs) instead of a simple HTML list, this way I could provide further instructions for modifying the functionality, however, AFAIK the plugin doesn't support this. Since the HTML list is so closely tied to javascript I find it acceptable to use the "js" extension.
As for your 2nd question, this is more difficult. You could pass a query parameter as Carlos mentioned. Alternatively you could create a new mime type, perhaps call it "autocomplete" and make an "index.autocomplete.erb" template. This should work, but I'm not sure if it's a best practice.
Your best railscast to date! Thanks for digging into the details of auto-completing virtual attributes, it is just what I needed!
Nice, Ryan.
This was a good one.
Hi Ryan,
Just wanted to say thanks for this awesome screencast, and for all the others on your site. It's great to see an enthousiast like yourself sharing knowledge with the rest of the community.
You screencasts make my monday-mornings considerably more pleasant ;-).
Hey Ryan,
I love RailsCasts but I have two comments around this particular episode:
1) I think that using "js" for the request really is wacky. The content type of the response is "text/javascript" indicating that the content really *is* javascript. Perhaps this is a place to register a new psuedo mime type?
2) You are probably open to escaping bugs when you are passing the search parameter through. What happens with your sample app if you type "?foo=bar" into your category box. You need to url encode the value.
Change element.value to encodeURIComponent(element.value).
The prototype way would be to replace the entire :with value with:
Object.toQueryString({search: element.value})
@Joe, good points. The more I think about it, a custom mime type would probably be a preferred solution here over using js. As mentioned above, ideally I would like to return javascript here with RJS, but AFAIK the plugin/scriptaculous doesn't support this. Someone please correct me if I'm wrong.
I hadn't thought about the escaping issue. I'll change the code in the show notes. Thanks!
Hey Ryan,
This may be beyond the scope of this tutorial, but how do you handle setting virtual attributes that are scoped to another model? Let's say you want to have several categories that belong to a bucket, and want to find_or_create_by_name_AND_BUCKET_ID. You can't pass the bucket info to the virtual attribute in the model. This question is much more about virtual attributes than auto-complete, but I've had it come up a lot in other situations as well. Thanks!
This is just what I was looking for - thank you, thank you.
Great screencast, but it really exposed how hackish the autocomplete plugin is.
Why isn't it a regular form builder like the other form fields? Customizing it shouldn't be so awkward (i.e. passing raw Javascript). Why can't the controller method use the virtual attribute on the model?
To be honest, using unobtrusive Javascript seems more straightforward than jumping through Ruby to accomplish this.
There is a real opportunity for someone to refactor this plugin and become a hero!
[Long time listener, first time caller...] Ryan, I swear I saw you type "field.{tab}" to trigger a snippet of a formated label+field. Is that a stock TextMate function or something you wrote for yourself?
@James, I'm not exactly sure if I understand your question. However, you could create a second virtual attribute for the bucket_id, save it to an instance variable, and use that in the category search.
@Geoffrey, I agree. I think they extracted this out into a plugin for a reason. I would love to see an updated version of it which flows better with modern techniques (such as REST).
@Bryan, it's a custom snippet I made. I use a lot of custom snippets and they are pretty easy to make. I recommend looking into it and developing your own library of snippets.
Ryan,
Sorry I wasn't clear.
When you create a setter method, they can only have one parameter (the value of the virtual attribute). How do you include other values from your controller in these setter methods?
def category_name=(name)
self.category = Category.find_or_create_by_name_and_parent_id(name, parent_id) unless name.blank?
end
Can't seem to figure out how to include additional variables (parent_id in this case) in these setters to keep them from being found if they 'belong_to' a different parent.
Same thing would occur if you were trying to tie a model object to "current_user".
@James, the model will need access to the parent_id somehow, so I recommend creating a 2nd virtual attribute which sets an instance variable.
The problem is there's no way to guarantee the parent_id attribute will be set before the category_name. To solve this, you can use a callback to handle the category setting. Check out this code.
http://pastie.caboo.se/184873
You may want to email me if this doesn't solve your problem: ryan[at]railscasts[dot]com
Ryan, great railscast as always, but I have a question. When I enter "categories.js" into my browser to display the list of autocomplete items, I get "No route matches '/categories.js' with {:method=>:get}". I don't think I have my routes set up properly. Did you add any routes besides the default ones? I'm not well versed in routing yet, so I'm not sure what I need to add.
@Dan - You'll probably want to add "format.js" to your respond_to block in your categories controller.
Thanks James; finally got it working! I had assumed I didn't need it since the file was index.js.erb instead of index.html.erb. Thanks for the help.
Ryan,
Thanks for the help, adding an additional virtual attribute and doing the find_or_create in a callback worked perfectly.
@Dan, Here's what my routes file looks like:
map.resources :products, :categories
This generates the formatted_catgories named route which should do what you want. I didn't need a respond_to block in my controller, but if you're not using Rails 2 you will need one.
Thanks for the screencast, it's great and very useful.
When I was trying this, I found a problem with my controller: It wasn't generating anything in the "/controller.js" step. So I put the following code in the end of the method:
respond_to {|format| format.js}
When I tested it, I was given a 'Template is missing' screen, asking for an 'layouts/application.js.erb'.
So I realized my controller has a unremoved "layout 'application'" line in it.
After I removed it, it all worked.
I don't know what really happened, but it may generate some headache for people who'd really need the "layout 'application'" line.
@Lucas - thanks for this 'semi' fix. I've been having problems with for the last day - no one else mentioned having problems so I thought my copy paste skills needed tuning...
I also have a layout :switch_layout (which is serves up a different layout depending on who's logged in) in my controller which is sending this Railscast off the tracks.
From what I've read of the above comments (Ryan, Joe and Geoffrey) - it would be cool to see a sudo mime type used and also a refactored auto_complete plugin...
Anyway, thanks for the screencast :D
Hey,
I can't find any contact details so I'll try my luck here.
There's hardly any resources or tutorials on developing the navigation for an application with rails.
Any subject you plan on taking on?
Kind regards
Aerpe
I think the main problem is that scriptaculous' Ajax.Autocompleter by default sets the target element's innerHTML to the response of the AJAX call.
Maybe an idea would be to override Ajax.Autocompleter's default onSuccess behavior so that it parses a, say, JSON string and turns it into a list before assigning it to the innerHTML of the target element. This way, it would be reasonable to have a mime type of text/javascript or even application/json because JSON is returned.
@Lucas, you shouldn't need to specify "layout :application" as this will be the default. However, if you do need to specify another layout you can do this:
http://pastie.caboo.se/185520
I would consider this to be a bug in Rails, maybe I'll get around to submitting a ticket or patch.
@Aerpe, thanks for the suggestion. I'll add it to my list. :)
Great screencast! I had this problem in one of the project I am working one. Thanks.
Thanks Ryan for yet another gem of a tutorial. keep up the great work.
This almost works, but it breaks support for tokens.
Instead of specifying :with, I use :param_name. This gives you the correct query string, it escapes the query automatically and tokens are supported.
<%= text_field_with_auto_complete :product, :category_name, { :size => 15 }, { :url => formatted_categories_path(:js), :method => :get, :param_name => 'search' } %>
@Stephen, thanks! I had no idea there was a :param_name option. That is much nicer. I'll update the code in the show notes.
Thanks Ryan, you're doing a great job!
I've got a little question: is there an easy way to combine "comlex forms" and "autocomplete with association"?
What I'm trying to do is a "purchase order" form with differents "purchases" (like a porject with different tasks) and I'd like to autocomplete the name of the product of each purchase
Thanks again
@FJuan, I haven't tried it, but you should be able to do this. The main thing you have to watch out for is that text_field_with_auto_complete can't be called through the form builder. This means you'll somehow have to get the name of the field to match what a normal text field would be. You may need to dig into the source code of text_field_with_auto_complete to figure out how to do this.
Great work Ryan! I'm a rails beginner and this website has been a huge help.
@FJuan, I'm trying to do a similar thing using the autocomplete in conjunction with Ryan's complex forms. I'm going to look into the source code as Ryan suggested, and hope I dont mess things up. Thanks again Ryan!
Thanks for this screencast - a really nice solution to drop down lists!
I'm having some troubles though... when I goto the url http://localhost:3000/categories.js nothing shows up... This is my dev.log
<pre>
Processing CategoriesController#index (for 127.0.0.1 at 2008-05-01 20:25:12) [GET]
Session ID: BAh7BzoMY3NyZl9pZCIlZGU1MWNkNzQ2MTYzYTQ0MmY3ZDZmNDhhMjljYjU2%0ANjYiCmZsYXNoSUM6J0FjdGlvbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhh%0Ac2h7AAY6CkB1c2VkewA%3D--9533e6fe70bd20e0155ec58df1e28d241ac42676
Parameters: {"format"=>"js", "action"=>"index", "controller"=>"categories"}
Category Load (0.000386) SELECT * FROM `categories` WHERE (name LIKE '%%')
Completed in 0.00667 (149 reqs/sec) | Rendering: 0.00005 (0%) | DB: 0.00039 (5%) | 406 Not Acceptable [http://localhost/categories.js]
</pre>
Can anyone see something going on?
From your log: "[http://localhost/categories.js]"
It would seem Rails is trying to load from port 80 (default for apache), not port 3000 (default for rails).
Furthermore, I believe categories are stored in {RAILS_ROOT}/public/javascripts/categories.js, not {RAILS_ROOT}/public/category.js where is appears you are trying to find it.
Can you post some of your code?
This is great. Thanks Ryan for an excellent screencast, and congrats on breaking 100 episodes.
I'm a bit confused with how to get this process to work with a model that relies on virtual attributes.
For example, I have a Person with :first_name and :last_name attributes, and I use a virtual attribute :name as a shortcut to concatenate them and make it easier to search.
Unfortunately, find_by_name and its dynamic cousins don't exist because in this case, :name is a virtual attribute of Person.
I was able to figure out what was happening before with the field not auto completing.. but now I'm having another problem.
When I hit the save button the category id isn't being stored in the product database.
the log says that category_name gets passed as a parameter but the relationship isn't saved - a new product is created with a NULL in category_id and a new category is created with a category_name.
Any ideas?
it possible to create more than only one attr_accessor ?? In my case, the "Category equivalent model", has 4 fields.
I know it sounds a bit purist, but using virtual attributes this way you're tightening your model with the view, aren't you?
Besides that, how can I pass two or more parameters in the ajax request with the :param_name option? I got it working with the :with option, but that is a bit ugly.
Great screencast... I was trying to get this to work for days and this made it really easy.
I want to take it a step further though, and allow people to enter the name, but actually save the ID of the found object back into the database, which would be useful when adding existing items to a list. I've gotten the results back as JSON, but now I'm wondering how to actually take them and create the list from the JSON rather than an HTML ul.
Does anybody know?
auto-complete. oh yes, this good!
but if i have 100 users on-line, this very-very slowly
I tried the autocompletion exactly like the screencast and it works perfectly ....except for safari ( 3.1.1) in production mode. I get no requests at all when typing in the autocomplete field.
It does work in development mode and it works in production mode for firefox, IE6 en IE7.
Any ideas ...?
Very Good!
tnks!
Dear Rayn (and Tim Cooper).
Thanks for the work you put into this is helps a lot.
I am having the same problems as tim (i.e. the category id is not being saved into the product model).
any ideas what this might be?
This is a really good example, but it is now more easily accomplished with model_auto_completer:
http://agilewebdevelopment.com/plugins/model_auto_completer
I'm attempting to use the auto-complete field to match related records that have a first_name and last_name field. In the profile model I've defined a full_name method that pairs the names: "#{self.first_name} #{self.last_name}"
I'm not sure if that is the proper way to accomplish what I need.
I've replaced all instances of name with full_name which is not working.
The virtual attributes in my other model are as follows:
def profile_name
profile.full_name if profile
end
def profile_name=(full_name)
self.profile=Profile.find_by_full_name(full_name) unless full_name.blank?
end
Now I know that the find_by_full_name is not correct,
Sorry, submitted the form before I was finished.
but I cannot seem to figure out how to combine the fields for use with auto-complete. I don't care if I'm searching on "first_name OR last_name OR (first_name AND last_name)" or something similar but it would be nice to use the full name.
hi Ryan! this was beautiful!
anwyay, i tried it on barebones projects and it worked! (only after i realized/overlooked a stupid mistake).
um, anyway, i was hoping i could incorporate it with your complex forms projects (episodes 73-75) and i tried and..... i can't get it to work. no matter how hard i squeezed my brain i can't seem to find the answer anywhere. i can't seem to make it cooperate with the "fields_for" method :(
the problem is that "text_field_with_auto_complete" doesn't seem to recognize for example, "auto_complete_field" of
<% fields_for "student[class_record_attributes][]" do |auto_complete_field| %>
when i use it like this:
<%= text_field_with_auto_complete :auto_complete_field, ...
any ideas? @_@
@bijou
I'm also trying to incorporate auto-complete with a similar setup to the one introduced in Complex Forms 1-3.
Anyone have any tips?
@Peter
i'm trying.... i tried to get it to work with text_field_tag, because in Ryan's example in this episode, it was used with the object-related text_field. i've put off using auto_complete for now. i need to finish other priorities for a project first before i go back to this... should i go back to this.
@ bijou and Peter
Just throwing in my chips to let you know I'm also trying to get text_field_with_auto_complete to work sensibly with fields_for.
However, unlike the Complex Forms 1-3 Railscasts, I'm not injecting fields_for with virtual attributes.
I'll post here if I can come up with a solution that isn't too embarrassing.
Is there a way to modify what is displayed in the <li> that is produced? For instance, if I was autocompleting a Name field, searching by lastname only, so I'm doing something like this:
<%= auto_complete_result @user, :lastname %>
Is there a way to display both the firstname and lastname in the <li> that is produced?
I've tried having my format.js respond by calling a partial which contains both the firstname and lastname, and it displays properly when I trigger the autocomplelte, but then when I submit the form, both the firstname and lastname get injected into my :lastname field
Is there a way to place many of these auto complete fields on one page? When I dynamically add more of these elements to my page (I need N topics with descriptions) the other auto complete fields don't work, I suspect because the div IDs are the same. Do you know of a way to fix this?
Hi, i followed the steps about the auto_complete and it works great!
but i need to add N more textfields with auto_complete in the same form (for example Complex Forms),
and i don't know how to do it, can you give me a hand with this? :(
Hi,
i want to get the same effect with that auto_complete text field, but using class instead id, is that possible?
Same Problem for me getting autocomplete to work with fields_for.
If anyone would get that to work I would appreciate a small hint on this thread :P
regards
Excellent. I've been searching for this for a long time. Thanks, Ryan!
Hi Ryan ,
all goes fine, but the up/down arrow keys were not working in IE when selecting the items in dropdown
thanks
i am a rewbie,
i am having trouble getting this to work, i get a
"wrong number of arguments (0 for 1)"
is there anyway to download the example files for this video? thanks for the videos, I look foward to getting this to work :)
Hi Ryan, your tutorial has just saved me a few hours of searching. Thanks a lot and best wishes from Germany!
Hi Ryan!
I tried to make your example work, but when i load the form, the text_field_with_auto_complete don't load the virtual attribute! Any ideas?!
for some reason I'm getting the following routes-related error: formatted_piece_url failed to generate from {:controller=>"piece", :action=>"show", :id=>:js}, expected: {:controller=>"piece", :action=>"show"}, diff: {:id=>:js}
Any idea why it keeps wanting to use the show action? I can't seem to figure how to get the route to go to the index action. My code here is just formatted_piece_url(:js). Can't seem to get this working until the routes are working, but everything else from the video is working fine! Any help?
If you get the "wrong number of arguments (0 for 1)" Error?
Add * to
def category_name(*name)
and the error will disappear
Thank you Ryan! for your efforts
Erez
Or don't forget the =
def category_name=(name)
how does this work if my model only has a unique ID column? the "name" is not unique.
Sorry if this discussion is very old by now... with regard to @bijou, @peter, @mike, etc.: I wrote a plugin recently that allows you to use text_field_with_auto_complete with fields_for so that it can be repeated multiple times in a single form.
See: http://github.com/pat11639/repeated_auto_complete
Hope this helps someone!
How can we add additional fields to the category object on the fly?
I mean if if the category doesn't exist based on the info that user typed in the autocompleter field, I would like to show Category form with name and description fields in place of autocompleter field and the category name should be prepopulated with the value that the user entered in the autocompleter field. Once the product is submitted along with the category name and description information, product should be either created/saved with category assigned to it.
Would appreciate it if somebody could point me to any existing resource that achieves this functionality. Thanks in advance.
-Satynos
I must say, that Pat's solution for multiple fields is really cool!
Thank you for this detailed information, it is done well.
Hello Ryan and guys,
I'm trying to integrate auto_complete feature with complex form (ep. #74) using repeated_auto_complete plug-in but looks like only the first field works.
Does anyone have successfully integrated dynamic fields with auto_completion ?
THANKS
Just a quick note: the link I pasted just above no longer works; for a modified auto_complete plugin that works in a complex form see:
http://patshaughnessy.net/repeated_auto_complete
I also posted a sample app showing how to use auto_complete for repeated fields, and fixed Sigfrid's issue as well.
When using Rails 2.3 it seems like the respond_to format.js is rendered with the application layout. Anyone that know why?
I'm very interested of finding a solution to the problem that Stephen Tudor describes in comment 39
@Martin.
I got round this problem with the respond_to block.
I added it at the end and specified that the .js format would have no layout :layout => false
@Martin
I run into the same issue using rails 2.2.2
@PhilCK
Your solution worked for me as well
How do you solve the 406 Not Acceptable [http://localhost/journals.js?search=Ab] error? I assume is some sort of routing problem.
Hi!
I work on a web application where I have sucessfully implemented this cool behavior.
Thanks Ryan!!!
BUT: I use this auto completion feature on a text field that queries a table with almost 4000 records ( an extensive list of restriction enzymes). The drop down menu goes way off screen at the bottom if the user only types in a single common letter.
I suppose I could limit the number of returned search results but that would possibly prevent the user from finding the correct value.
Is there a way to introduce a scroll bar to the drop down menu?
Cheers Juergen
Hi @Ryan,
A breakthrough functionality I got working for myself in a jiffy. Many thanks for your railcasts...
I am using auto_complete on a search field which auto completes a list of tag.names and company.names for Company model. And it is returning for me a collected list of matching Company or Tag names for the search string.
BUT when i select one value from the Autocomplete list, previous string in the autocomplete text-field gets vanished and the selected word takes over.
What if I need multi word values to be submitted in that search field with commas between them ?
like "Comment, Private, Nitin"
And I also got the autocomplete to work after putting a comma..
Help!
Nitin R
Hi Ryan,
Thanks so much for the screencast - It was a massive help.
Recently i refactored my db, and changed the relationship between my 'product' and 'category' from has_many to has_many :through, using the second example in your 'two many-to-many' screencast.
I'm now having trouble getting my app top work - wondered if you had any ideas?
I get an "undefined local variable or method `category' for #<product:0x4cdca2c>"
Thanks a million,
Gonzo
Hi Ryan:
Wonderful episode. I seem to have uncovered a minor glitch. Perhaps it only happens in my app but I wanted to make sure.
Let's say you create a product with a category. Then you try to edit the product and empty the category field and hit 'Submit'. The product will be saved but will still show the previously stored category.
This may be the case for me because I used your nifty_generator scaffold to set things up. Any ideas?
@Eric in post 55.
If you're wishing to have the list return virtual atributes such as full_name rather than a field such as surname of first_name then I found I had to modify the auto_complete_macros_helper.rb file
I modified it so that it sent the method rather than retrieved the field name. I changed line 98 to
items = entries.map { |entry| content_tag("li", phrase ? highlight(entry.send(field), phrase) : h(entry.send(field))) }
THis allowed me to access virtual attributes.
@Juergen post 82. I manage to get the autocomplete working nicely with Thinking_Sphinx. Here's my auto-complete-search:
www.crystalpalacelocal.co.uk
If you search for your enzymes this way then you can limit the number of results back but have the most relevant higher up the list depending upon your search weighting factors.
Hope this helps.
Thanks Ryan for the screencast. Yet again I owe you more than one beer for all this.
I'm still a bit of a newbie I guess because there are lots of surprises for me in rails development. Anyways wanted to mention that in order to get this to work I had to add a new layout called categories.js.erb, which was empty except for the yield tag. Otherwise when I went to localhost/categories.js the file would get run through the same layout as the html views, which is obviously unwanted. If there's a way around that I would appreciate the update.
thanks for the helpful info Ryan!
for those who were asking how to pass id instead of name because there are many situation where you cannot find a unique entity by name, here is the trick i have used make it work for me.
http://pastie.org/486924
Hope that helps.
Hey Ryan!
You don't even need to create the :js output for the category index action, you can use the native method (auto_complete_for :category, :name) provided by the plugin by using the :url parameter of the view helper:
:url => url_for(:controller => :categories, :action => :auto_complete_for_category_name, :escape => false)
Cheers, Sazima
works fine for me by setting the correct route in routes.rb.
sample:
map.tags '/tags/:js', :controller => 'tags', :action => 'index'
(tags are what categories are in the example above)
Greets, Jens
As usual, gr8 work!!
Is it possible for you to do an episode on in_place_editor? It looks like they have a plugin for it, but none of them have a good documentation.
Thanks and keep up the good work.
Muntasir
Hey all, I'm having a bit of problem with this thing returning the data into the intended <div> no problem. However the actual div never becomes visible.
I'm using Rails 2.3.3 and Ruby 1.9.1. I have prototype and effect.js loaded.
I figured it out.
In Ruby 1.9, the to_s member of arrays changed. (So did the default functionality of spitting out an array directly).
You need to add .join to the end of of items.uniq in the autocompletehelper.rb.
As in "items.uniq.join" instead of just "items.uniq", so search and replace that you'll be fine.
Ryan,
Great screencast. I'm having problems though. Instead of categories I'm using departments. I'm also using NetBeans 6.7 for my IDE. Everything is going great until I load the edit page, then in the NetBeans Console is an error:
DEPRECATION WARNING: formatted_departments_path() has been deprecated. Please pass format to the standard departments_path method instead..
I'm not sure where to go from here.
John M
Great podcast, Ryan. I was wondering why this wouldn't work if the Product model has a has_one association to the Category model and the Category model has a belongs_to to the Product.
I've tested both ways and It works the way you have it, but was just curious if you knew why this other way would not work.
Thanks Ryan! I'm a big fan of Railscasts.
I'm running into a snag. http://localhost:3000/companies.js is not rendering the html as expected and has <li> and <ul> take in it. When I look at the source with firebug, I see <pre> tags are included in the output. I'm new to this, and think there may be an easy fix or obvious oversight.
Any help is greatly appreciated!
@92 John McLeod
To get rid of the Deprecation Warning, I think you need do this:
Change THIS
:url => formatted_..._path(:js) ..
to THIS
:url => ... _path(:format => :js) ..
(FYI I'm running Rails 2.3.4)
Ryan - Superb Web resource you've got here! Well Done - My first port of call when I get stuck developing my Apps. Keep up the good work :-)
This works Great...
Until I try to edit a record with a Category assigned. In my case the system produces a "SystemStackError in Inventories#edit" stack level too deep
I'm using this code: <%= text_field_with_auto_complete :inventory, :tag_name, { :size => 15 }, { :url => formatted_tags_path(:js), :method => :get, :param_name => 'search' } %>
It works with unpopulated data in create and edit, it does not edit existing data and always produces the Stack level too deep message
Anyone else having this problem?
tried http://pastie.org/184873 (unable to get it to work)
Problem:
I am trying to use autocomplate, for the nested/parent resource places, when creating an shift.
If the place exsists, it works
if the create action is called, I get the error:
error:
Mysql::Error: Column 'user_id' cannot be null: INSERT INTO `places` (`name`, `created_at`, `updated_at`, `user_id`) VALUES('Herlevvidovre', '2009-11-28 10:42:48', '2009-11-28 10:42:48', NULL)
My setup:
map.resources :users do |users|
users.resources :pays
users.resources :places
users.resources :shifts do |shifts|
shifts.resources :payroles
end
end
model:
def place_name
place.name if place
end
def place_name=(name)
self.place = Place.find_or_create_by_name(name, @user) unless name.blank?
end
places controller:
index:
@places = @user.places.find(:all, :conditions => ['name LIKE ?', "%#{params[:search]}%"])
create:
@place = @user.places.build(params[:place])
shift form view:
<%= text_field_with_auto_complete :shift, :place_name, { :size => 15 }, { :url => formatted_user_places_path(@user, :js), :method => :get, :param_name => 'search' } %>
can cascading select be implemented using auto_complete.
i mean, i have country and state fields and i want auto_complete options to show from state field based on the value of country field.
is it possible?
thanks in advance
How did you all solve the 406 Not Acceptable error? I see that others have it but haven't figured out how to fix it. Thanks!
@106 Libby
Try to add the following line to the respond_to method in your controller:
format.js {render :html=>@your_collection_of_DB_results}
It fixed similar issue for me.
BTW, I started experiencing this problem as soon as I updated the gems on my PC. A bit embarrassing.
@AleBoi Thanks! That plus Pat's fields_for fork fixed all my issues. Thanks for your help all
Hi there, thought I might try to contribute something.
I followed this screencast and then tried to upload my project to my heroku (these guys rock!!) hosting which uses postsql databases. I found that the search was only return case sensitive results instead of case insensitive results, so I dug into it and on PostSQL you can just replace the LIKE with ILIKE in your controllers query for the same results.
Also a note on CSS styling.
You can override the inline css styling if you want more control over the popup form by adding :skip_style => true
<%= text_field_with_auto_complete :product, :category_name, {:size => 30}, {:skip_style => true, :url => formatted_categories_path(:js), :method => :get, :param_name => 'search' } %>
/* Autocomplete Popup Styling, Notice I changed the color of li.selected to red? */
div.auto_complete {
width: 350px;
background: #fff;
}
div.auto_complete ul {
border:1px solid #888;
margin:0;
padding:0;
width:100%;
list-style-type:none;
}
div.auto_complete ul li {
margin:0;
padding:3px;
}
div.auto_complete ul li.selected {
background-color: #990404;
}
div.auto_complete ul strong.highlight {
color: #800;
margin:0;
padding:0;
}
Hello all, and thanks Ryan for your great work with these episodes. I've managed to make that work, but slightly modified to work with jQuery.
There's a little trap when using virtual attributes in a way like this
def document_name=(name)
self.document = Document.find_by_name(name)
end
In face, Model.find_or_create_by_name() is handy, but when you pass no name, no Model is being created by ActiveRecord. Though, self.document = XX is still being affected, and a Document object is thought to be present in the parent model. But, in this case, XX is empty (no Document found, and no Document saved to the database yet), thus XX.id is not present, and that leads to strange behaviors as the XX Document is not a "classic Document". Calling document_path(parent_model.document) immediately after the parent_model update or save, causes crashes. Immediately after means, in the partial I'm trying to generate for the response to the browser, in my AJAX context.
I hope my explanation was not too messy to be understood.
I had the hardest time figuring out why I couldn't get any of the Javascript to run. I finally got it to work by restarting the server after issuing the
script/plugin install git://github.com/rails/auto_complete.git
Ryan, thanks a bunch for this. It's been such a great help. One question: I am running into trouble when my associated entity already has an index.html action. Can you please tell me how to inform the controller of how to discriminate between index.html and index.js?
thank you!
Nice Thing, but it find_or_create_by won't work when you have validations on your category model for additional fields.
I tried to prefill them also, but accessing attributes from the form won't work.Creating a virtual attribute was no solution because then i would need to enter information twice.
Is it just me... or when I double-click on one of the resulting values, I get the values twice in the text field, sometimes half of the 2nd one ?!?! Weird!
Chris
What do I do to display in the #show?
Right now, it shows empty. And I cannot use the virtual attribute. Should that be working?
I am getting the following error ... Not sure how to fix it ...Looks like the .js is called but is unable to render ....
Processing ClientsController#index to js (for 127.0.0.1 at 2010-05-23 05:59:27) [GET]
Parameters: {"search"=>"A"}
[4;36;1mClient Load (1.0ms)[0m [0;1mSELECT * FROM `clients` [0m
Completed in 19ms (View: 3, DB: 2) | 406 Not Acceptable [http://localhost/clients.js?search=A]
arch=A]
Great info, helped me a lot.
However I can't make 'tokens' work while using Rails 2.3.8 with recent plugin. Neither original nor param_name variation worked for me. I wonder if recent version of this plugin still supports tokens.
@136, I am getting error with formatted_categories_path
"formatted_categories_path" has to be changed to "categorie_spath(:format => :js)"...
...and...
in you category_controller.rb you have to specifiy another format:
respond_to do |format|
format.html # index.html.erb
format.js # index.js.erb
end
Several other users have mentioned it, and there has been no resolution posted.
When I go to http://localhost/categories.js, as shown in the demo, nothing is listed and a 406 error is returned in the logs.
Can someone provide a solution for this?
Alright, I figured out the 406 problem for other people who run in to it.
This is not a server-side issue, but rather an error in a presumption made in this example.
Odds are you're implementing this for a controller that already exists, and it already has a "def index" section.
For example, my code already had this:
# GET /users
# GET /users.xml
def index
@users = User.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @users }
end
The 406 is occurring because this def index doesn't have a way to respond to javascript requests.
So after the format.xml line, add this:
format.js { @categories = Category.find(:all, :conditions => ['name like ?', "%#{params[:search]}%"]) }
VIOLA!
Justin Zollars,
i've got the same issue,but i fixed it in my code.
you may have a model called TagName, so it is looping when you do a method call tag_name in your Inventory model.
Use the method :tag_name2 and try it out, like this:
<%= text_field_with_auto_complete :inventory, :tag_name2, { :size => 15 }, { :url => formatted_tags_path(:js), :method => :get, :param_name => 'search' } %>
hope i helped
i installed jquery-rails and tried autocompletion, but i've got "Ajax is not defined" error,
some help? thnx!
Thanks that was great Ryan
But I got some error when I try to write:
When I try to open the products page its showing the following error:
Any help with this I will be grateful.
Thx a lot
hi ryan
i try this with mongodb mongoid gem but i m unable to found solution for find
@categories = Category.find(:all, :conditions => ['name LIKE ?', "%#{params[:search]}%"])
i replace it by
@search = params[:search]
@tags = Category.where({name:/"#{@search}"/})
buts its unable to search any result
can you help me on same
thanks in advance
Hello!!!,
I'm not able to use autocomplete, I'm getting the following issue;
ReferenceError: Ajax is not defined
TKS :)