js.erb没有执行javascript但是处理了rails

LpL*_*ich 4 javascript ruby jquery ruby-on-rails

我正在尝试使用will_paginate或kaminari使用Ajax进行分页.

当我点击分页链接我的日志说

Processing by Instructor::FullDashboardController#pupil_leads as JS
Run Code Online (Sandbox Code Playgroud)

Rendered instructor/full_dashboard/pupil_leads.js.erb within layouts/main (0.1ms)
Run Code Online (Sandbox Code Playgroud)

但是js没有效果.所以为了测试我写的

console.log("Hello");
<%logger.debug "This is the js file"%>
alert('Hello Rails');
Run Code Online (Sandbox Code Playgroud)

在js.erb文件中,在我的日志中,我看到This is the js file了预期,但在浏览器的js控制台中没有任何内容,并且警报未显示.所以我知道文件正在处理中,因为我的logger.debug工作但是没有js.

我该如何进一步排除故障?

Rails 4.1

Ruby 2.1

完整的短跑控制器

class Instructor::FullDashboardController < ApplicationController
   layout 'main'
...
   def pupil_leads
     @ivar = Model.where("something = ?", some_object.id).order("created_at desc").page(params[:page]).per(10)
     respond_to do |f|
       f.js { render :content_type => 'text/javascript' }
       f.html
     end
  end
Run Code Online (Sandbox Code Playgroud)

Зел*_*ный 19

layout: false向渲染块添加选项:

def pupil_leads
   # some code here
  respond_to do |f|
    f.js { render layout: false, content_type: 'text/javascript' }
    f.html
  end
end
Run Code Online (Sandbox Code Playgroud)

出于某种原因,Rails不会将请求识别为xhr,我还注意到必须完整指定视图扩展名(.erb.html.slim).

  • 还要记住,js中的单个错误和所有错误都可能无声地失败 - 即使是第一行的初始console.log语句也是如此.从js文件中的console.log语句开始,然后慢慢地添加片段,直到console.log语句没有出现 - 这意味着在您粘贴的最新代码中的某处存在无法处理的(in rails)错误. (3认同)
  • 对于调试异步操作,请使用"Chrome /网络"响应选项卡,在这里您始终可以看到服务器响应有什么问题. (2认同)