gpt4 book ai didi

jquery - Rails 3.1 : Server always processing as HTML, 不是 JS

转载 作者:行者123 更新时间:2023-12-01 00:49:09 24 4
gpt4 key购买 nike

我正在使用 Rails 3.1.3。我想按以下步骤使用 js.erb 文件:
1.在posts Controller 中创建测试动​​作:

def test
respond_to do |format|
format.js
end
end

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

resource :posts do
collection do
get 'test'
end
end

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

$('#abc').append('<h1>something</h1>');

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

<div id='abc'></div>
<%= link_to "Test", test_posts_path, :remote => true %>

但是,我运行并单击测试链接,它会转到 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

所以,服务器不会像JS一样处理,test.js.erb文件中的jQuery不会被执行。你能为我解释一下为什么服务器以 HTML 方式处理,而在 Controller 中我定义了 respond_to format.js?
我假设项目中的所有响应都定义为 format.html,所以我的测试操作无法响应 JS?如果正确的话我可以更改吗?
非常感谢

更新:在 asset/javascripts/application.js 中需要使用以下行:

//= require jquery
//= require jquery_ujs

然后就可以了。

最佳答案

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

关于jquery - Rails 3.1 : Server always processing as HTML, 不是 JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9239891/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com