web-workers in Rails projects

梦想与她 提交于 2020-01-01 11:51:53

问题


I try to use web workers to be able to run several javascript codes at the same time. To start the worker I put the following into my javascript code section:

var worker = new Worker("my_task.js");
worker.onmessage = function(e) {
    console.log('Worker said: ', e.data);
};
worker.postMessage("test"); 

my_task.js is the same folder as the file that holds the code above. This is how my_task.js looks like:

self.addEventListener('message', function(e) {
  self.postMessage(e.data);
}, false);

But there seems to be a problem with Ruby on Rails (version 3.2.1):

Started GET "/users/my_task.js" for 127.0.0.1 at ...
...
...
ActiveRecord::RecordNotFound (Couldn't find User with id=my_task)

As you can see Rails tries to send an http get request when the script is started.
How can I solve this problem? Is there a better way to use web workers in Rails projects?

Thanks


回答1:


  • Put my_task.js in /public/javascripts
  • In your javascript code where the worker is created, use the following ERB: var worker = new Worker("<%= javascript_path "my_task.js" %>");
  • Add .erb to the extension of whatever file the above javascript code is found in. So if it's mypage.js, it becomes mypage.js.erb.

The above works for me on Rails 4.2.0, ruby 2.0.0p598




回答2:


Put my_task.js in the public folder.




回答3:


using the above <% javascript_path %>

and lib/assets/javascripts worked for me using Rail5



来源:https://stackoverflow.com/questions/14955948/web-workers-in-rails-projects

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