我正在使用嵌套的局部函数在视图中显示数据,并且我注意到,当我离开页面然后返回到页面时,我的javascript被多次触发:
<!-- index.html.erb -->
<div class="col-12 material">
<%= render 'meals', meals: @meals %>
</div>
<!-- end index.html.erb -->
<!-- _meals.html.erb -->
<div class="p-0" id="isotope">
<% meals.each do |meal| %>
<%= render "meals/meal", meal: meal %>
<% end %>
</div>
<script>
$(document).on('ready turbolinks:load', function() {
console.log("_meals partial")
})
</script>
<!-- end _meals.html.erb -->
<!-- _meal.html.erb -->
<%= link_to meal_path(meal[:slug]) do %>
<div class="item col-12 col-sm-6 col-md-4 col-lg-3 animated fast invisible p-2 a b <%= meal[:type_of] %>">
<div class="card m-0 my-lg-3 flex justify-content-between">
<div class="row flex align-items-center">
<div class="col-4 col-sm-12">
<%= image_tag meal[:image_link], class: "card-img-top ml-2 mt-2 ml-sm-0 mt-sm-0" %>
</div>
<div class="card-body col-8 col-sm-12">
<h5 class="card-title font-medium pb-3">
<%= meal[:name] %>
</h5>
<!-- <p class="card-text d-none d-sm-block">
<%#= meal[:description] %>
</p> -->
</div>
</div>
<div class="card-footer text-muted justify-content-center row">
<span style="margin: 2px;" class="badge bg-info-gradiant pointer"><%= meal.common_name %></span>
</div>
</div>
</div>
<% end -%>
<!-- end _meal.html.erb -->
如果我正在查看index.html.erb
并点击刷新,则会阅读"_meals partial"
1x 。如果我点击一顿饭,然后单击链接(<%= link_to "Menu", meals_path %>
)返回,我读到"_meals partial"
2x 。如果我重复此过程,则会读取"_meals partial"
3x ,依此类推...
为什么会这样?我该怎么做才能解决此问题?
编辑:
作为测试,我删除了Turbolink,现在没有发生此问题。我怀疑这是Turbolink的预期功能,因此config可能会出问题,从而使Turbolink重新加载我页面的某些部分吗?