我应该按什么顺序编写Javascript(Coffeescript)才能在Rails 4中执行代码?

我应该按什么顺序编写Javascript(Coffeescript)才能在Rails 4中执行代码?,javascript,jquery,ajax,coffeescript,ruby-on-rails-4,Javascript,Jquery,Ajax,Coffeescript,Ruby On Rails 4,我可以成功地生成ajax生成我的div,如下所示: // This is a manifest file that'll be compiled into application.js, which will include all the files // listed below. // // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,

我可以成功地生成ajax生成我的div,如下所示:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
视图:

spectrum_identification_results.js.coffee:

$ ->   
  $("#sir_table").dataTable
    sPaginationType: "full_numbers"
    bJQueryUI: true
    bProcessing: true
    bServerSide: true
    sAjaxSource: $('#sir_table').data('source')
$ ->
  $("a[data-sir-id]").click ->
  alert "whassup"
使用json对象和所有我设法生成的datatable服务器端处理和ajax

没关系。但是在我创建的ajax表中,我需要另一个指向ShowAction的ajax链接。 远程链接的
格式如下:

link_to( "#{sir.sir_id}", "results/#{sir.id}", :data => {'sir-id' => "#{sir.id}"}, remote: true )
如果我删除
remote:true
并使其非ajax工作,它就可以正常工作了

这是我希望它触发的javascript:

spectrum_identification_results.js.coffee:

$ ->   
  $("#sir_table").dataTable
    sPaginationType: "full_numbers"
    bJQueryUI: true
    bProcessing: true
    bServerSide: true
    sAjaxSource: $('#sir_table').data('source')
$ ->
  $("a[data-sir-id]").click ->
  alert "whassup"
但它永远不会被执行。我认为这可能与rails4 turbolinks gem有关,它只触发$(document)。第一次加载时就准备好了,但我不确定


关于为什么会发生这种情况和/或如何执行javascript的任何线索?

Rails会自动将资产目录中的所有内容编译到头部应用程序文件(例如application.js和application.css)中。我会检查并确保在application.js中有如下内容:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .

然后,如果您的所有JavaScript和CoffeeScript文件都在“javascripts”目录下,那么一切都应该正常工作。如果链轮设置不正确(例如,在执行生成项目的
rails new
命令时跳过JavaScript),那么一切都不会正确编译。

我认为我的资产/javascripts/application.js没问题,因为我使用的是spectrum\u identification\u results.js.coffee(同一控制器)这可能是因为您在另一个实例中直接引用它,而不希望它像在这个实例中那样被动加载。只要确保链轮和CoffeeScript导轨设置正确,一切都应该正常。尝试制作一个项目的空白副本,其中包含:
railsnewdemo-B--skip keeps-T--no rc
,以及项目中的端口,其中“demo”等于项目名称。如果您没有使用ActiveRecord,请在该命令中插入
-O