Reputation: 72
i am developing a basic depot application with rails i have added a button
<%= button_to 'Add to Cart', line_items_path(product_id: product) , remote: true %>
when i click on home page hyperlink then click on the button again , ajax gets duplicated , if i click on home page 3 times , the button ajax call 3 times and so on
Note : the problem disappear when i remove jquery-ujs from application.js
line_item controller
def create
@cart = current_cart
product = Product.find(params[:product_id])
@line_item = @cart.add_product product
@line_item.save
respond_to do |format|
if @line_item.save
format.html { redirect_to store_url }
format.js { @current_item = @line_item }
format.json { render :show, status: :created, location: @line_item }
else
format.html { render :new }
format.json { render json: @line_item.errors, status: :unprocessable_entity }
end
end
end
home page action
def index
@products = Product.order(:title)
@cart = current_cart
end
application.js
// This is a manifest file that'll be compiled into application.js,
// listed below.
//
//= require jquery
//= require turbolinks
//= require jquery-ui
//= require jquery_ujs
//= require_tree .
ajax response should render this js file show.js.erb
$('#cart').html("<%=j render @cart %>");
//#START_HIGHLIGHT
$('#current_item').css({'background-color':'#88ff88'})
.animate({'background-color':'#114411'} , 1000);
using gem jquery rails 4.0.4
Upvotes: 3
Views: 655
Reputation: 72
i found the problem . using turbolink with javascript link tags outside head load an extra jquery script every time i click on home page , a dummy solution is to put javascript link tag inside the head itself
Upvotes: 2