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

发布于 2024-11-27 14:45:30 字数 1700 浏览 0 评论 0原文

我正在将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遇见了你 2024-12-04 14:45:30

试试这个:

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

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

link_to 参考

Try this:

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

if you used route helpers, passing it as a hash wouldn't be necessary

link_to reference

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文