js.erb不执行javascript,但已处理过的rails [英] js.erb not executing javascript but is processed rails
问题描述
我正在尝试使用will_paginate或kaminari与Ajax进行分页.
I'm trying to get my pagination working with Ajax, using either will_paginate or kaminari.
当我点击分页链接时,我的日志显示
When I hit pagination link my log says
Processing by Instructor::FullDashboardController#pupil_leads as JS
和
Rendered instructor/full_dashboard/pupil_leads.js.erb within layouts/main (0.1ms)
但是js无效.因此,为了测试它,我写了
But the js has no effect. So to test it I wrote
console.log("Hello");
<%logger.debug "This is the js file"%>
alert('Hello Rails');
在js.erb文件中,在日志中按预期方式看到了This is the js file
,但是浏览器中的js控制台中没有任何内容,并且警报未显示.所以我知道文件正在处理中,因为我的logger.debug有效,但是没有js.
In the js.erb file, in my log I see This is the js file
as expected, but there is nothing in js console in my browser and the alert doesn't show. So I know the file is being processed, as my logger.debug works but there is no js.
如何进一步解决此问题?
How can I further troubleshoot this?
Rails 4.1
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
推荐答案
在渲染块中添加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
出于某种原因,Rails无法将请求识别为xhr
,我还注意到必须完全指定视图扩展名(.erb.html
或.slim
).
For some reason Rails don't recognize request as xhr
, I also watched that the views extension (.erb.html
or .slim
) must be specified in full.
这篇关于js.erb不执行javascript,但已处理过的rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!