Rails Ajax:.js.erb呈现为文本,未执行

时间:2011-08-04 08:18:59

标签: jquery ruby-on-rails ajax link-to

我正在使用javascript_include_tag :defaults的jquery-rails gem。因此页面有标题:

<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: 200content-type: text/javascript。 Chrome JavaScript控制台没有显示任何错误。

我尝试了以下解决方法:

  • 包括rails.js ---失败
  • link_to更改为<div onclick="$.ajax(...)"> ---工作

我对:remote =>link_to的行为感到非常困惑。

1 个答案:

答案 0 :(得分:0)

试试这个:

<%= link_to 'Run', { :controller => "config_panel", :action => "full_run" }, :remote => true %>

如果您使用了路由助手,则无需将其作为哈希传递

link_to reference