How to link drop-down lists in the form of rails

How can I link the dropdowns on the rails page. both dropdowns will come from the database.

For example, if it first drops out category(the coming form is categorytable). the second drop, productsalso coming from db, will be populated based on the selection of the first drop down list?

+3
source share
2 answers

You can create a drop-down list using the collection_select helper and when you select a value in one folder, you can send an ajax request back to your controller action to refresh another page element with a new drop-down list and products, for example

<%= collection_select(:category, :some_category_method_name, 
    Category.all, :id, :category_name, 
    {:prompt => 'Select'},
    { :onchange =>  remote_function(:url => {:action => 'get_products'}, 
    :with => "'id=' + this.value")}) 
%>
<div id='product_dropdown'></div>

, , , , 'get_products' . "product_dropdown" , .

 def get_products
   @category = Category.find(params[:id)
   render :update do |page|
     page.replace_html 'product_dropdown', 
          :partial => 'partial_name_in_which_you_have_product_drop_down',
          :locals => {:products => @category.products}

   end
 end

, .

, collection_select,

+3

Javascript, .

o ne YUI. , , .

0

Source: https://habr.com/ru/post/1732292/


All Articles