Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I took the following tutorial as template to rewrite my cascading dropdown:</p> <p><a href="http://homeonrails.blogspot.de/2012/01/rails-31-linked-dropdown-cascading.html" rel="nofollow">http://homeonrails.blogspot.de/2012/01/rails-31-linked-dropdown-cascading.html</a></p> <p>So, now I throw all the different models into one array and filter it by appending names to the class, to differentiate not only by ID, but also by name. Also the jQuery Plugin chainedTo makes the code much more readable.</p> <p>So, the controller looks now like this:</p> <pre><code> @types_for_dropdown = [[I18n.t("text archive_database"),"ori"],[I18n.t("text query_database"),"mod"]] @tables_for_dropdown = [] @ad_qry_mods = AdQryMod.all @ad_qry_mods.each do |i| @tables_for_dropdown = @tables_for_dropdown &lt;&lt; [i.qry_mod_text,"mod#{i.id}",{:class =&gt; "mod"}] end @ad_ori_tbls = AdOriTbl.all @ad_ori_tbls.each do |i| @tables_for_dropdown = @tables_for_dropdown &lt;&lt; [i.otb_filename,"ori#{i.id}",{:class =&gt; "ori"}] end @fields_for_dropdown = [] @ad_qry_flds = AdQryFld.all @ad_qry_flds.each do |i| @fields_for_dropdown = @fields_for_dropdown &lt;&lt; [i.qry_fieldname,i.id,{:class =&gt; "mod#{i.ad_qry_mod_id}"}] end @ad_ori_flds = AdOriFld.all @ad_ori_flds.each do |i| @fields_for_dropdown = @fields_for_dropdown &lt;&lt; [i.otb_fieldname,i.id,{:class =&gt; "ori#{i.ad_ori_tbl_id}"}] end </code></pre> <p>And the form looks like this:</p> <pre><code>&lt;%= content_for :head do %&gt; &lt;script&gt; $(document).ready(function(){ $('select#audit_function_database_1_id').chainedTo('select#audit_function_database_1'); $('select#audit_function_field_1_id').chainedTo('select#audit_function_database_1_id'); $('select#audit_function_database_2_id').chainedTo('select#audit_function_database_2'); $('select#audit_function_field_2_id').chainedTo('select#audit_function_database_2_id'); }); &lt;/script&gt; &lt;% end %&gt; &lt;div class="grid-6-12"&gt; &lt;%= f.input :database_1, label: I18n.t("field_label audit_function database_1"), hint: I18n.t("field_hint audit_function database_1"), as: :select, collection: @types_for_dropdown, include_blank: true %&gt; &lt;/div&gt; &lt;div class="grid-6-12"&gt; &lt;%= f.input :database_2, label: I18n.t("field_label audit_function database_2"), hint: I18n.t("field_hint audit_function database_2"), as: :select, collection: @types_for_dropdown, include_blank: true %&gt; &lt;/div&gt; &lt;div class="grid-6-12"&gt; &lt;%= f.input :database_1_id, label: I18n.t("field_label audit_function database_1_id"), hint: I18n.t("field_hint audit_function database_1_id"), as: :select, collection: @tables_for_dropdown, include_blank: true %&gt; &lt;/div&gt; &lt;div class="grid-6-12"&gt; &lt;%= f.input :database_2_id, label: I18n.t("field_label audit_function database_2_id"), hint: I18n.t("field_hint audit_function database_2_id"), as: :select, collection: @tables_for_dropdown, include_blank: true %&gt; &lt;/div&gt; &lt;div class="grid-6-12"&gt; &lt;%= f.input :field_1_id, label: I18n.t("field_label audit_function field_1_id"), hint: I18n.t("field_hint audit_function field_1_id"), as: :select, collection: @fields_for_dropdown, include_blank: true %&gt; &lt;/div&gt; &lt;div class="grid-6-12"&gt; &lt;%= f.input :field_2_id, label: I18n.t("field_label audit_function field_2_id"), hint: I18n.t("field_hint audit_function field_2_id"), as: :select, collection: @fields_for_dropdown, include_blank: true %&gt; &lt;/div&gt; </code></pre> <p>This is really a nice solution and I can recommend it to everyone!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload