Rails 3 Link_to:远程未触发 RJS

发布于 2024-10-09 14:09:07 字数 1830 浏览 2 评论 0原文

我正在使用以下代码在 Rails 3 中设置 AJAX 操作。代码的 AJAX 部分似乎可以工作,但它没有请求正确的文件,而我的 respond_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。

我觉得我在路由或链接语法中遗漏了一些东西,但我可以在网上和文档中找到的所有示例似乎都准确地显示了我正在做的事情。有什么问题吗?

谢谢。

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 →', 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 &#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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

∝单色的世界 2024-10-16 14:09:07

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

= link_to 'Add a zone →', new_zone_path(:format => :js), :remote=>true

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

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

= link_to 'Add a zone →', 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.

烟─花易冷 2024-10-16 14:09:07

这个问题也可以用这个语法来解决(包括 data-type 声明)

= link_to 'Add a zone →', new_zone_path, remote: true, "data-type" => "script"

如果你只有 .js.erb 文件,没有 .html 要渲染的文件,无需分配请求的数据类型(Rails 自动检测 JS 唯一的响应方式)

要点 这里你必须确保你的请求被认为是作为JS执行。这意味着您在控制台中的请求应如下所示:

Started ...
Processing by ABCController#method as JS

This problem also can be solved with this syntax (includes data-type declaration)

= link_to 'Add a zone →', new_zone_path, remote: true, "data-type" => "script"

In case you just have .js.erb file , no .html file to render, it's not necessary to assign the data-type of request (Rails automatically detect that JS only way to response)

The main point here is you have to make sure that your request is considered to be executed as JS. It means your request in console should look like this:

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