ruby-on-rails 在Rails 5中单击浏览器上的后退按钮时,带有Select2的表单会重复

rxztt3cl  于 9个月前  发布在  Ruby
关注(0)|答案(6)|浏览(101)

_header.html.erb(用于表单部分)

<%= form_for home_path, class: 'home', role: 'search', method: :get do |f| %>
<div class="form-group" style="display:inline;">
 <div class="input-group input-group-md">
 <%= text_field_tag :q, params[:q], placeholder: ... ,class: 'form-control hideOverflow', type: "search" %>
 <%= select_tag "category", options_from_collection_for_select(...),include_blank: true, class: 'form-control hideOverflow', type: "search" %>
 <%if logged_in? %>
  <%= select_tag "location", options_for_select([...], ...),class: 'form-control hideOverflow', type: "search" %>
 <% else %>
  <%= select_tag "location", options_for_select([...], ...),class: 'form-control hideOverflow', include_blank: true, type: "search" %>
 <% end %>
  <span class="input-group-addon"><%= submit_tag "Search", class: "btn-transparent"%></span>
 </div>
</div>
<% end %>

字符串

JS代码

<script>
 $( document ).on('turbolinks:load', function() {
    $('select#category').select2({ 
        width: '60%', 
        dropdownAutoWidth : true, 
        placeholder: "Choose a category",
        maximumSelectionLength: 3
    }); 
    $('select#location').select2({
        width: '40%', 
        dropdownAutoWidth : true, 
        minimumResultsForSearch: Infinity
    });
 });

</script>

毛刺或渲染问题(点击链接查看图片)

normal
after I click back button from the browser
有人可以帮助我为什么?另外,我的搜索表单在标题部分文件的导航栏中。
如果我在脚本中去掉$(...).select,一切都很好.我认为select.js有问题

b0zn9rqh

b0zn9rqh1#

标签:https://stackoverflow.com/a/41915129/5758027
我在自己的代码中使用了这个解决方案:

$(document).on('turbolinks:before-cache', function() {     // this approach corrects the select 2 to be duplicated when clicking the back button.
        $('.select-select2').select2('destroy');
        $('.select-search-select2').select2('destroy');
    } );

字符串
一个观察者:

$(document).ready( ready );                  //... once document ready
    $(document).ajaxComplete( ready );           //... once ajax is complete
    $(document).on('turbolinks:load', ready );   //... once a link is clicked

    function ready() {
        $(".select-search-select2").select2({
            theme: "bootstrap",
            language: 'es',
            allowClear: true
        });

        $(".select-select2").select2({
            theme: "bootstrap",
            language: 'es',
            minimumResultsForSearch: Infinity,
            allowClear: true
        });
    };

ohfgkhjo

ohfgkhjo2#

清除该高速缓存所有的时间使使用涡轮链接实际上没有意义?
不如这样吧?

$(document).on('turbolinks:before-cache', function(e) {
  return $('.form-control.select2').each(function() {
    return $(this).select2('destroy');
  });
});

字符串

jobtbby3

jobtbby33#

我没能解决这个渲染问题(仍在等待正确的答案!),但如果有人和我一样有类似的问题,请尝试跳出框框思考。以下是我的技巧:我在应用程序中添加了一个后退按钮。

获取完整url路径

# get the previous url 
def save_previous_page
    session[:return_to] = request.fullpath
end

字符串

仅当页面不是主页或搜索页面时才显示后退按钮

<% if session[:return_to] != request.fullpath%>
   <%= link_to session.delete(:return_to) || request.fullpath, class: 'back-button' do%>
     <i class="fa fa-arrow-circle-left" aria-hidden="true"></i>
   <%end%>
<% end %>


与此同时,我仍然在等待,并试图修复渲染问题.

已修复问题

只需将此代码添加到.js文件中即可

Turbolinks.clearCache();

ktecyv1j

ktecyv1j4#

这很可能是一些资产不一致,你应该检查你的app\views\layouts文件夹中是否有重复的jQuery,jQuery UJS或Turbolinks声明。检查你页面的所有<script>标签,如果你在layout文件夹和内部视图中声明了相同的脚本。如果不是这样,检查renderyieldbuild呼叫

kq4fsx7k

kq4fsx7k5#

简单的解决方案,不要在你不希望它运行的东西上运行select2构建器。

$("select#category:not(.select2-container):not(.select2-hidden-accessible)").select2();

字符串

gg58donl

gg58donl6#

Rails 7更新

这里的很多东西在Rails 7中都不起作用,特别是turbolinks:before-cache事件。您正在寻找的新事件是turbo:before-cache,和turbo:load,因此它看起来像这样:

$(document).on("turbo:before-cache", function() {
    $("#select_id").select2('destroy');
});

$(document).on('turbo:load', function() {
    $('#select_id').select2();
});

字符串

相关问题