Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamic form with ajax
    primarykey
    data
    text
    <p>I have a form for my Item model for it's :make attribute. Based on the option chosen it should render the corresponding Products that share the same :item_make value. So if you select "Hoodies" on the first form, the different hooded sweatshirts available would show on the second. Any help would be greatly appreciated.</p> <p>quote</p> <pre><code>attr_accessible :items_attributes has_many :items </code></pre> <p>product</p> <pre><code>attr_accessible :name, :item_make has_many :items </code></pre> <p>item</p> <pre><code>attr_accessible :make, :product_id belongs_to :quote belongs_to :product </code></pre> <p>the two inputs:(inside nested quote form -> Item form)</p> <pre><code>&lt;%= f.input :make, collection: clothing_types, label: 'Thread Type', remote: true, url: url_for(controller: 'quotes', action: 'update_products'), type: 'json', input_html: {class: "select_make"} %&gt; &lt;%= f.input :product_id, input_html: {class: "select_product"} %&gt; </code></pre> <p>Currently @products is the list of all products.</p> <p>quotes_Controller:</p> <pre><code>def new @quote = Quote.new @quote.items.build @item = Item.new @products = Product.all end def update_products @make = params[:make] @subproducts = Product.where(:item_make =&gt; @make).all render json: @subproducts.map{|x| [x.id, x.name]} end </code></pre> <p>**EDIT:**Application.js</p> <pre><code>$(document).on('change', '.select_make', function() { $.ajax({ type: 'GET', url: "/quotes/update_products", success: function(data) { if (data.content == 'None') { $('.select_product').empty(); $('.select_product').append( $('&lt;option&gt;No make provided!&lt;/option&gt;')); } else $('.select_product').empty(); $('.selecT_product').append(); //unfinished } }); }); </code></pre> <p>I've tried using different variations of the jquery here <a href="https://stackoverflow.com/questions/6863990/ruby-on-rails-drop-down-box-on-change-event">Ruby on Rails - drop down box on change event</a> but haven't been able to get it to work yet.</p> <p>If anyone could enlighten me on the javascript needed to do this, that would be awesome</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.
 

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