从 RJS 渲染时,Rails 中的 link_to_remote 渲染错误的 URL
我有一个“来宾”对象的部分,它呈现一个指向销毁操作的链接,如下所示:
<%= link_to_remote "remove", :url => { :action => "destroy", :id => guest.id } %>
当从 ERB 视图调用时,它工作正常,例如
<div id="guests">
<%= render :partial =>; @event.guests %>
也就是说,它呈现如下内容:
<a href="#" onclick="new Ajax.Request('/guests/destroy/10', {[options removed]}); return false;">remove</a>
问题是,当由于像这样的创建操作而从 RJS 呈现此部分时:
page.insert_html :bottom, :guests, :partial => @guest
它不是链接到销毁操作,而是链接到来宾的详细信息/显示操作,例如,
a href="#" onclick="new Ajax.Request('/events/1/guests/10', {[options removed]}); return false;">remove</a>
谁能告诉我我做错了什么,或者是否有办法调试这里发生的事情?正如您可能知道的那样,我是 Rails 的新手;)
I have a partial for a "guest" object which renders a link to a destroy action like this:
<%= link_to_remote "remove", :url => { :action => "destroy", :id => guest.id } %>
When called from an ERB view it works fine, e.g.
<div id="guests">
<%= render :partial => @event.guests %>
That is, it renders something like:
<a href="#" onclick="new Ajax.Request('/guests/destroy/10', {[options removed]}); return false;">remove</a>
The problem is, when this partial is rendered from RJS as a result of a create operation like this:
page.insert_html :bottom, :guests, :partial => @guest
Instead of linking to the destroy action, it links to the detail/show action for the guest instead, e.g.
a href="#" onclick="new Ajax.Request('/events/1/guests/10', {[options removed]}); return false;">remove</a>
Can anyone tell me what I'm doing wrong, or if there's a way to debug what's going on here? As you might be able to tell, I'm a complete newbie to Rails ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你做得几乎是正确的,但你并没有完全遵循惯例。请允许我解释一下。
根据 HTTP 规范,链接被设计为 GET 请求 - 数据检索。您不应该使用链接来修改资源的状态。基本上,不要使用链接进行潜在的破坏性操作,因为它们可能会被蜘蛛或加速器跟踪。 Rails 默认将每个链接设为 GET 请求。因此,当您执行此操作时:
它发出了 GET 请求。我假设你的routes.rb 文件中有类似
map.resources :guests 的内容,
这会创建大量你可以使用的路由,如下所示:
对于Rails 中的资源路由,所使用的方法决定了所调用的控制器操作。
使用 GET 请求调用的 URL /guests/1 调用 SHOW 操作。您需要在 link_to_remote 调用中指定方法 - :get、:put、:post 或 :delete。 :put 用于更新,:post 用于创建,:delete 用于删除。
或更干净,
但说真的,不要使用链接。使用按钮。
希望有帮助!
You were doing it almost right, but you're not quite following convention. Allow me to explain.
Links are designed, by HTTP specification, to be GET requests - data retrieval. You are not supposed to use links to modify a resource's state. Basically, don't use links for potentially destructive actions, as they could be followed by a spider or accelerator. Rails defaults to making every link a GET request. So, when you did this:
It made a GET request. I assume you have in your routes.rb file something like
map.resources :guests
This creates a ton of routes that you could use like this:
With resource routes in Rails, the METHOD used determines the controller action called.
The URL /guests/1 called with a GET request calls the SHOW action. You need to specify the method in your link_to_remote call - either :get, :put, :post, or :delete. :put is for updates, :post is for creates, :delete is for, well, deleting.
or cleaner,
But seriously, don't use links. Use buttons.
Hope that helps!
事实证明“销毁”在 Rails 中具有特殊含义。我将销毁操作重命名为删除,并且效果很好。
Turns out "destroy" has special meaning in Rails. I renamed the destroy action to delete instead and it works fine.