I am trying to execute a simple AJAX request using link_to: remote and display the response dynamically. The problem is that I get 5 responses instead of one. Why can this happen?
page.html.erb:
<%= link_to item.title, item_path(item, :format => :js), :remote => true %>
show.js.erb:
$("<%= escape_javascript render(:file => 'items/show.html.erb') %>").insertAfter('#sortable');
$('#show_item').slideDown();
items_controller.rb:
def show
@item = Item.find(params[:id])
respond_to do |format|
format.html
format.js
end
end
Update: I am using jQuery. Gemset includes Devise, paperclip and simple_form. I also have a similar problem when using: confirm with link_to. The fact is that this confirmation dialog is displayed 5 times no matter what you click.
There is only one element in the generated html with the 'sortable' id:
<ul id="sortable">
<li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span><a href="/items/10.js" data-remote="true">Another item</a></li>
<li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span><a href="/items/9.js" data-remote="true">test</a></li>
</ul>
source
share