简体   繁体   English

第二次访问页面时未应用Rails CSS / JS

[英]Rails css/js not being applied when visiting page for second time

I am trying to implement the chosen plug-in and a datepicker for my rails application and having an issue where on the second page visit (not page reload, reloading the page actually refreshes the plug-ins and brings them back), the plug-ins are appearing and then disappearing very quickly, almost as if they are getting reset. 我正在尝试为我的Rails应用程序实现所选的插件和一个日期选择器,并且遇到一个问题,即在第二页访问时(不是重新加载页面,重新加载页面实际上会刷新插件并将其带回),插件ins出现然后很快消失,几乎就像它们被重置一样。

I've tried searching for this issue but I haven't really seen anything on rails, I think it has to do with the way the scripts are served and bootstrap's css/js files, but I'm not really sure and can't pinpoint it. 我已经尝试搜索此问题,但是我并没有真正看到任何东西,我认为这与脚本的提供方式和引导程序的css / js文件有关,但是我不确定并不能精确定位。

I am also getting [cache miss] on my page loads, if that makes a difference. 如果这有所作为,我的页面也会加载[cache miss]。

Things I've tried: 我尝试过的事情:

  • Switching the order of my requires in both application.js and application.css 在application.js和application.css中切换我的需求顺序
  • Moving the jQuery to initialize the plug-ins directly to the _form.erb.html page 移动jQuery将插件直接初始化到_form.erb.html页面
  • Moving the jQuery into a document.ready in application.js 将jQuery移至document.ready的application.js中
  • Removing said document.ready (and using document.bind) 删除所述document.ready(并使用document.bind)
  • Using a setTimeout in the document.ready as a quick fix to set them back after they get "reset" (no dice here) 在document.ready中使用setTimeout作为快速修复方法,以在它们“重置”后重新设置它们(此处没有骰子)
  • Deleting a partial when I suspecting the partial's js was overriding the .chosen and .datepicker for some weird reason (not the case) 当我怀疑部分的js由于某些奇怪的原因而覆盖了.chosen和.datepicker时,删除了一个部分(并非如此)

Here's some of the code, I feel like this is something so stupid and simple I am missing. 这是一些代码,我觉得这太愚蠢和简单,我想不到。

//= require rails-ujs
//= require turbolinks
//= require jquery.min
//= require jquery_ujs
//= require jquery-ui
//= require_tree .
//= require chosen.jquery.min
//= require bootstrap-datepicker

$(document).ready(function(){

    $('.chosen').chosen(
        {   
            allow_single_deselect: true,
            no_results_text: 'No results matched',
            width: '200px'
        }
    );

    $('.datepicker').datepicker();


});

and the application.css 和application.css

 /*
 *= require_tree .
 *= require_self
 *= require chosen.min
 *= require datepicker
 */

the offending _form.html.erb 令人反感的_form.html.erb

  <div class="field">
    <%= form.label :customer_id %>
    <%# form.text_field :customer_id, id: :order_customer_id %>
    <%= collection_select( :order, :customer_id, @customers, :id, :name,{selected: order.customer_id, prompt: false}, { class: 'chosen' }) %>
  </div>

  <div class="field">
    <%= form.label :date %>
    <%# form.date_select :date, id: :order_date, class: 'datepicker' %>
    <%= form.text_field :date, default: 'Pick a date', class: 'datepicker' %>
  </div>

I think turbolinks make it. 我认为Turbolink做到了。 You can confirm that by remove //= require turbolinks line in application.js file and restart your application. 您可以通过删除//= require turbolinks application.js文件中的//= require turbolinks行并重新启动应用程序来确认。 If you really need turbolinks in your application, check out bellow link to fix this problem. 如果您的应用程序中确实需要涡轮链接,请查看下面的链接以解决此问题。

Rails 4: how to use $(document).ready() with turbo-links Rails 4:如何在Turbo-Links中使用$(document).ready()

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM