Angelo Cordova
2011-Nov-10 20:55 UTC
Auto populate text_fields based on selected item from another collection_select
Hello people I''m trying to figured this out, but I still can''t do it. I have a rails 3 app, I''m working with invoices and payments. In the form for payments I have a collection_select where I display all the invoices number (extracted from a postgres database), and what I''m trying to do is when i select an invoice autopopulate others text_fields (provider, address, etc.) without reloading the page, in the same form. I know I should use ajax, js, jquery, but I''m a beginner in these languages, so i don''t know how or where to start hope you can help me... thanks -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Xuan
2011-Nov-11 09:13 UTC
Re: Auto populate text_fields based on selected item from another collection_select
I would do it as follows: + Set a jquery event on your select in some .js file: $("#your_select_id").on("change", function(){ $.ajax({ url, data, etc... checj jquery docs }); }); I suggest .on() assuming you are using jquery 1.7. Alternatively you can check .delegate() or .bind() if you are using older versions. + Controller: def updating_action [...] respond_to do |format| format.js end end + View updating_action.js: Updating jquery and rails code such as $(id).val("<%=escape_javascript(@value)%>") Check .val(), .replaceWith(), .text() and other similar jquery methods to help you update your view. Alternatively you can handle response on your ajax call, but I''m used to do it like this, which seems to me easier and more flexible. Maybe someone else can simplify it even more... Anyway, hope it helps. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Seemingly Similar Threads
- collection_select not selecting the selected item
- update form text_field on collection_select selection
- Auto-populating time portion of a datetime field from a form
- Obtain selected value from collection_select
- how to obtain the selected value from collection_select