Rails Ajax:.js.erb 呈现为文本,未执行
我正在将 jquery-rails gem 与 javascript_include_tag :defaults
一起使用。因此,页面有标题:
<script src="/javascripts/jquery.js?1312437063" type="text/javascript"></script>
<script src="/javascripts/jquery_ujs.js?1312437070" type="text/javascript"></script>
<script src="/javascripts/application.js?1311268601" type="text/javascript"></script>
<meta name="csrf-param" content="authenticity_token"/>
<meta name="csrf-token" content="KdgrBxF726xfPP3qbNOHd/0TG9c7lVCoZDXPnadFOzI="/>
Ajax 请求来自带有以下行的视图:
<%= link_to 'Run', :controller => "config_panel", :action => "full_run", :remote => true, :format => :js %>
它呈现为:
<a href="/config_panel/full_run.js?remote=true">Run</a>
在控制器中定义的响应:
def full_run
respond_to do |format|
format.js { render :text => "alert();"}
format.xml
end
end
单击链接时,服务器记录:
Started GET "/config_panel/full_run.js?remote=true"
Processing by ConfigPanelController#full_run as JS
Parameters: {"remote"=>"true"}
Rendered text template (0.0ms)
Completed 200 OK in 1ms (Views: 0.6ms | ActiveRecord: 0.0ms)
浏览器显示纯文本:
alert();
HTML 标题有 status: 200< /code> 和
内容类型:text/javascript
。 Chrome JavaScript 控制台没有显示错误。
我尝试了以下解决方法:
- includerails.js --- 未能
- 将
link_to
更改为-- - 工作
我对 link_to
中 :remote =>
true 的行为感到非常困惑。
I'm using jquery-rails gem with javascript_include_tag :defaults
. Hence the page has header:
<script src="/javascripts/jquery.js?1312437063" type="text/javascript"></script>
<script src="/javascripts/jquery_ujs.js?1312437070" type="text/javascript"></script>
<script src="/javascripts/application.js?1311268601" type="text/javascript"></script>
<meta name="csrf-param" content="authenticity_token"/>
<meta name="csrf-token" content="KdgrBxF726xfPP3qbNOHd/0TG9c7lVCoZDXPnadFOzI="/>
Ajax request comes from the view with the line:
<%= link_to 'Run', :controller => "config_panel", :action => "full_run", :remote => true, :format => :js %>
It's rendered as:
<a href="/config_panel/full_run.js?remote=true">Run</a>
Response defined in controller:
def full_run
respond_to do |format|
format.js { render :text => "alert();"}
format.xml
end
end
When link clicked, the server logs:
Started GET "/config_panel/full_run.js?remote=true"
Processing by ConfigPanelController#full_run as JS
Parameters: {"remote"=>"true"}
Rendered text template (0.0ms)
Completed 200 OK in 1ms (Views: 0.6ms | ActiveRecord: 0.0ms)
And browser shows plain text:
alert();
The HTML header has status: 200
and content-type: text/javascript
. And Chrome JavaScript console shows no error.
I tried the following workaround:
- include rails.js --- failed
- change
link_to
to<div onclick="$.ajax(...)">
--- worked
I'm very confused by the behavior of :remote =>
true in link_to
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
如果您使用路由助手,则不需要将其作为哈希传递
link_to 参考
Try this:
if you used route helpers, passing it as a hash wouldn't be necessary
link_to reference