sometimes javascript run perfect and sometimes not in ruby on rails 4

…衆ロ難τιáo~ 提交于 2019-12-22 08:31:02

问题


I used datepicker in calendar it shows perfect but not shows the arrow button to change month. and also give error sometimes like this:

ActionController::RoutingError (No route matches [GET] "/assets/images/ui-icons_ef8c08_256x240.png")

datepicker.js

$(document).ready(function(){
  $('[data-behaviour~=datepicker]').datepicker();
});

css link

sorry for too long question and tell me if i miss something.

Big thanks in advance

Edit: I observed that when i delete the public/assets directory and then enter rake assets:precompile this time error is not showing but arrow still is not showing.


回答1:


It looks like turbolinks problem which are added to rails 4 by default. In turbolinks $(document).ready is not always firing. You can see more information about turbolinks and some suggestions how to use them here railscast or you can add to some links

data: { no_turbolink: true }

You can also just remove the turbolinks from gemfile if you don't need them. Also check if you have the ui-icons_ef8c08_256x240.png in

/assets/images/



回答2:


You can also follow the instruction here. Basically you have to tell javascript to also execute on page:load.

Credit to Meltemi

Coffee:

ready = ->

  ...your coffeescript goes here...

$(document).ready(ready)
$(document).on('page:load', ready)

Javascript:

var ready;
ready = function() {

  ...your javascript goes here...

};

$(document).ready(ready);
$(document).on('page:load', ready);

Rails 4: how to use $(document).ready() with turbo-links




回答3:


First, install jquery-turbolinks gem. And then, don't forget to move your included Javascript files from end of body of your application.html.erb to its <head>.

As described here, if you have put the application javascript link in the footer for speed optimization reasons, you will need to move it into the tag so that it loads before the content in the tag. This solution worked for me.



来源:https://stackoverflow.com/questions/23254379/sometimes-javascript-run-perfect-and-sometimes-not-in-ruby-on-rails-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!