条件:在 link_to_remote 中确认

发布于 2024-08-10 09:21:20 字数 376 浏览 7 评论 0原文

我有一个 Rails 应用程序,在删除之前,我在其中执行了一个常规删除对话框,其中包含“您确定吗”。一切都很好,但我还有一个小的 ajax,点击即可删除包含所有信息的块。无论发生什么,该块都会被删除,这会让事情变得混乱。我似乎无法在网上找到任何有关如何在确认符号后有条件地执行操作的文档。

我的代码如下所示:

 <%= link_to_remote "Delete", :url => 
   {:controller => "pixel", :action => :destroy, :id => pixel.id}, 
   :onclick=>"$(this).up(0).remove()" %>

谢谢!

I have a Rails app where I was doing a general delete dialog with "are you sure" before it gets deleted. That's all fine, but I also have a small ajax on click to remove the block containing all the information. The block gets removed no matter what which makes things confusing. I couldn't seem to find any documentation online on how to conditionally execute an action after the confirm symbol.

My code looked like this:

 <%= link_to_remote "Delete", :url => 
   {:controller => "pixel", :action => :destroy, :id => pixel.id}, 
   :onclick=>"$(this).up(0).remove()" %>

Thanks!

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

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

发布评论

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

评论(3

南风几经秋 2024-08-17 09:21:20

你能发布你的代码吗?我想这样的事情会起作用:

link_to_remote("Delete item", :url => item_path, :method => 'delete', :confirm => 'Are you sure?')

Can you post your code? I'd imagine something like this would work:

link_to_remote("Delete item", :url => item_path, :method => 'delete', :confirm => 'Are you sure?')
dawn曙光 2024-08-17 09:21:20

我现在没有时间继续举例,但请尝试使用 link_to_remote:complete 选项在操作完成后删除元素,而不是 :onclick

http://api.rubyonrails.org/classes/ActionView/Helpers/ PrototypeHelper.html#M001645 - 查看“回调”部分。

它可能很简单:

<%= link_to_remote "Delete", :url => 
   {:controller => "pixel", :action => :destroy, :id => pixel.id}, :confirm => "You sure?",
   :complete =>"$(this).up(0).remove()" %>

I don't have time to pursue an example right now, but try using the :complete option to link_to_remote to remove the element after the action completes, instead of :onclick.

http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M001645 - look at the "callbacks" section.

It's probably as simple as:

<%= link_to_remote "Delete", :url => 
   {:controller => "pixel", :action => :destroy, :id => pixel.id}, :confirm => "You sure?",
   :complete =>"$(this).up(0).remove()" %>
嗫嚅 2024-08-17 09:21:20

我可以提出一个小建议吗,理想情况下不应使用基于默认路由(控制器、方法、id)的路由,而是使用命名路由或在声明路由时免费提供的适当的静态路由(例如地图) .resources :pixels)

我强烈建议阅读rails 路由指南,它是一个很好的摘要可以做什么以及如何做。

至于你的问题,你想要实现的目标都是基于强迫性的javascript。这并不是说 link_to_remote 方法不好,但如果您显示列表,它肯定会输出混乱的代码。在我看来,首先实现一个没有 javascript 的工作示例,然后不引人注目地添加 javascript 会更干净、更好。这会增加开销吗?是的,但是您的代码会变得更干净,并允许您轻松添加额外的逻辑(例如删除已删除的行),而不必在每一行上重复功能。

抱歉,我没有足够的时间来提供示例,但是简单的 google 搜索不显眼的 javascript Rails 会产生很多结果,但是 这里有一个可以帮助您入门。

希望这会有所帮助,尽管它有点偏离主题:)

Can I make a small suggestion, using routes which are based on the default routes (controller, method, id) should ideally not be used, instead use named routes or the proper restful routes given for free when you declare your routes (eg. map.resources :pixels)

I would highly recommend reading the rails routing guide, its a great digest of what can be done and how to do it.

As for your question, what you are trying to achieve is all based on obtrusive javascript. Thats not to say that the link_to_remote method isn't great, but it certainly outputs messy code if you are displaying a list. I would be cleaner a nicer, in my opinion, to first implement a working example with no javascript then add the javascript unobtrusively. Does this add overhead? yes, but your code will come out cleaner and allow you to easily add extra logic (like removing the row you have deleted) without having to duplicate the functionality on every line.

Sorry but I do not have enough time to provide examples, but a simple google search for unobtrusive javascript rails yields many many results, but here is one to get you started.

Hope this helps, even though its a bit off topic :)

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