Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating multiple Ajax buttons with a each loop
    primarykey
    data
    text
    <p>I have two models : User and Product. One user has many owned_products, and a product belongs to an owner. The products have a <code>available:boolean</code>.</p> <p>I want to make a list of owned_products that can be toggled from available to unavailable with a button. Here is what I did, using the <a href="http://ruby.railstutorial.org/chapters/following-users?version=4.0#sec-a_working_follow_button_with_ajax" rel="nofollow">M. Hartl example</a> :</p> <p><strong>app/views/shared/_owned_products_list.html.erb</strong></p> <pre class="lang-rb prettyprint-override"><code>&lt;ol class="products"&gt; &lt;% @owned_products.each do |product| %&gt; &lt;%= link_to(product.name, product) %&gt; &lt;%= render 'products/available_form', product: product %&gt; &lt;% end %&gt; &lt;/ol&gt; </code></pre> <p><strong>app/views/products/_available_form.html.erb</strong></p> <pre class="lang-rb prettyprint-override"><code>&lt;div id="available_button_&lt;%=product.id%&gt;"&gt; &lt;% if product.available? %&gt; &lt;%= form_for(product, remote: true) do |f| %&gt; &lt;div&gt;&lt;%= f.hidden_field :available, value: nil %&gt;&lt;/div&gt; &lt;%= f.submit t('product.available.undo'), class: "btn btn-small" %&gt; &lt;% end %&gt; &lt;% else %&gt; &lt;%= form_for(product, remote: true) do |f| %&gt; &lt;div&gt;&lt;%= f.hidden_field :available, value: true %&gt;&lt;/div&gt; &lt;%= f.submit t('product.available.do'), class: "btn btn-primary btn-small" %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;/div&gt; </code></pre> <p><strong>app/controllers/products_controller.rb</strong></p> <pre class="lang-rb prettyprint-override"><code> . . . def update @product = Product.find(params[:id]) if @product.update_attributes(product_params) respond_to do |format| format.html do flash[:success] = t('flash.success.product.update') redirect_to @product end format.js end else render 'edit' end end . . . </code></pre> <p><strong>app/views/products/update.js.erb</strong></p> <pre class="lang-rb prettyprint-override"><code>$("#available_form_&lt;%=@product.id%&gt;").html("&lt;%= escape_javascript(render('available_button', product: @product)) %&gt;") </code></pre> <p>But it doesn't work : The available button is not refreshed at all :</p> <p>When I click on the available (or unavailable) button, nothing change. If I refresh the whole page, it toggles, regardless the number of clicks...</p> <p>Do you know where I failed ?</p> <p><strong>EDIT</strong></p> <p>OK, I got it, it was a dumb fault : my available_form id was <code>available_button_&lt;%=@product.id%&gt;</code> and not <code>available_form_&lt;%=@product.id%&gt;</code>...</p> <p>So here is the right one :</p> <p><strong>app/views/products/update.js.erb</strong></p> <pre class="lang-rb prettyprint-override"><code>$("#available_button_&lt;%=@product.id%&gt;").html("&lt;%= escape_javascript(render('available_button', product: @product)) %&gt;") </code></pre>
    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.
    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