开发者

Rails Ajax: .js.erb rendered as text, not executed

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 开发者_JAVA百科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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜