Rails 3.1:服务器总是以HTML格式处理,而不是JS

ban*_*hay 3 jquery ruby-on-rails

我正在使用rails 3.1.3.我想使用js.erb文件,如下步骤:
1.在posts控制器中创建测试操作:

def test
    respond_to do |format|
        format.js
    end
end  
Run Code Online (Sandbox Code Playgroud)

2.在routes.rb中定义此操作:

resource :posts do
    collection do
        get 'test'
    end
end
Run Code Online (Sandbox Code Playgroud)

3.创建test.js.erb文件:

$('#abc').append('<h1>something</h1>');
Run Code Online (Sandbox Code Playgroud)

4.在index.html.erb文件中:

<div id='abc'></div>
<%= link_to "Test", test_posts_path, :remote => true %>
Run Code Online (Sandbox Code Playgroud)

但是,我运行并单击以测试链接,它转到带有URL的空白页面,http://localhost:3000/posts/test日志服务器是:

Started GET "/posts/test" for 127.0.0.1 at Sat Feb 11 18:04:36 +0700 2012
Processing by PostsController#test as HTML
Run Code Online (Sandbox Code Playgroud)

因此,服务器不作为JS处理,test.js.erb文件中的jQuery不会被执行.你能解释为什么在控制器中我将服务器进程作为HTML来定义respond_to format.js吗?
我假设项目中的所有响应都定义为format.html所以我的测试操作无法响应JS?如果正确,我可以改变吗?
非常感谢你

更新:在assets/javascripts/application.js中需要使用的行:

//= require jquery
//= require jquery_ujs
Run Code Online (Sandbox Code Playgroud)

然后它工作.

osa*_*oun 6

你确定你gem 'jquery-rails'的Gemfile和<%= javascript_include_tag "jquery", "jquery_ujs" %>你的布局或index.html.erb模板中有吗?