本文介绍了Rails 3 Link_to:remote不会触发RJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码在rails 3中设置AJAX动作.该代码的AJAX部分似乎可以正常运行,但是它并没有请求正确的文件,而我的response_to会以常规HTML格式为其提供服务.

路由信息:

resources :zones do
  resources :records
end

控制器:

def new
  @zone = Zone.new
  respond_to do |format|
    format.html
    format.js
  end
end

链接视图(haml):

= link_to 'Add a zone →', new_zone_path, :remote=>true

从link_to生成的HTML(还请注意html实体渲染失败...但这就是另一个问题):

<a href="/zones/new" data-remote="true">Add a zone &amp;#8594;</a>

对于踢球,列出视图/区域的目录.我不确定我这样做是否正确,所以我既有new.js.rjs也有new.rjs.它们都具有相同的内容,但不会被动作拾取.

|   `~zones/
|     |-_form.html.haml
|     |-_record.html.haml
|     |-edit.html.haml
|     |-index.html.haml
|     |-new.html.haml
|     |-new.js.rjs
|     |-new.rjs
|     `-show.html.haml

最后,单击链接后的服务器日志:

Started GET "/zones/new" for 127.0.0.1 at Wed Dec 29 00:04:03 -0700 2010
  Processing by ZonesController#new as */*
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1
Rendered zones/_form.html.haml (22.1ms)
Rendered zones/new.html.haml within layouts/application (34.9ms)
Completed 200 OK in 80ms (Views: 42.0ms | ActiveRecord: 0.4ms)

如您所见,它正在为请求呈现.html文件.现在,为了进行测试,我点击了页面 http://localhost:3000/zones/new.js 直接.它提供了new.js.rjs.此外,javascript远程调用正在运行. Firebug显示请求和响应,但请求的页面错误.

我也为了测试而这样做:

= link_to "Add a zone", '/zones/new.js', :remote=>true

对于javascript来说,哪种方法工作正常(rjs已下载并执行并且可以正常工作),但是对于禁用javascript的系统而言,它没有很好的故障转移功能.

我正在使用jquery进行值得的操作.

我觉得我在路由或链接语法中缺少某些内容,但是可以在网上和文档中找到的所有示例似乎都在准确显示我在做什么.有什么收获?

谢谢.

解决方案

您需要明确告诉Rails您想要js格式:

= link_to 'Add a zone &#8594;', new_zone_path(:format => :js), :remote=>true

作为一种解释:您必须指定.js扩展名,因为Rails不能区分.在许多情况下,您可能希望使用Ajax来获取html或json,而不仅仅是javascript. Rails允许您以任何格式获取任何内容,这就是为什么必须指定它的原因.

I'm working to setup an AJAX action in rails 3 with the following code. The AJAX part of the code seems to work, but it does not request the correct file and my respond_to serves it the regular HTML.

The routing information:

resources :zones do
  resources :records
end

controller:

def new
  @zone = Zone.new
  respond_to do |format|
    format.html
    format.js
  end
end

Link in view (haml):

= link_to 'Add a zone &#8594;', new_zone_path, :remote=>true

Generated HTML from link_to (also notice the failed rendering of the html entity...but thats another issue):

<a href="/zones/new" data-remote="true">Add a zone &amp;#8594;</a>

For kicks, a directory listing of the view/zones. I'm not sure I am doing this quite right,so I have both new.js.rjs and new.rjs. They both have the same content, but are never picked up by the action.

|   `~zones/
|     |-_form.html.haml
|     |-_record.html.haml
|     |-edit.html.haml
|     |-index.html.haml
|     |-new.html.haml
|     |-new.js.rjs
|     |-new.rjs
|     `-show.html.haml

Lastly, the server log from when I click the link:

Started GET "/zones/new" for 127.0.0.1 at Wed Dec 29 00:04:03 -0700 2010
  Processing by ZonesController#new as */*
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1
Rendered zones/_form.html.haml (22.1ms)
Rendered zones/new.html.haml within layouts/application (34.9ms)
Completed 200 OK in 80ms (Views: 42.0ms | ActiveRecord: 0.4ms)

As you can see, it is rendering the .html file for the request. Now, for testing, I hit the page http://localhost:3000/zones/new.js directly. And it serves up new.js.rjs. Also, the javascript remote call is working. Firebug shows the request and response, but its requesting the wrong page.

Also for testing I did this:

= link_to "Add a zone", '/zones/new.js', :remote=>true

Which works fine (rjs is downloaded and executed and works correctly) for the javascript but it doesn't have the nice failover for javascript-disabled systems.

For what it is worth I am using jquery.

I feel like I am missing something in the routing or the link syntax but all the examples I can find online and in the documentation seem to show exactly what I am doing. Whats the catch?

Thanks.

解决方案

You need to explicitly tell Rails that you want the js format:

= link_to 'Add a zone &#8594;', new_zone_path(:format => :js), :remote=>true

As a way of explanation: You have to specify the .js extension because Rails doesn't discriminate. In many cases, you might want to fetch html or json with Ajax--not just javascript. Rails will let you fetch whatever content in whatever format, which is why you have to specify it.

这篇关于Rails 3 Link_to:remote不会触发RJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 15:07