在rails控制器,如何防止双提交(当用户双提交提交按钮或命中两次输入)? [英] In rails controllers, how to prevent double submit (when user double-clic submit button or hit enter twice)?

查看:96
本文介绍了在rails控制器,如何防止双提交(当用户双提交提交按钮或命中两次输入)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,一切都在标题,但我会解释一点: - )



我的rails应用程序包含许多形式(Ajaxified或不)。 >

为了防止用户提交两次或多次表单,我使用了Javascript。



我有一个Ajaxified表单的场景:




  • 用户提交表单(clic或enter)

  • javascript禁用提交按钮

  • rails控制器执行操作(例如Soap请求或DB中的插入)

  • rails控制器更新页面并启用提交按钮if必要(出现错误时)



现在我要添加服务器端代码



有任何建议吗?

解决方案

尝试使用Redis锁定,并用类似

  Redis.current.lock(#{current_user.id} .action_name )do 
#一些代码
end

m使用
https://github.com/mlanett/redis-lock


Well, everything's in the title but I'll explain a little more :-)

My rails app contain many forms (Ajaxified or not).

To prevent users to submit twice or more some forms, I use Javascript.

There's my scenario for a Ajaxified form :

  • the user submit the form (clic or enter)
  • the javascript disable the submit button
  • the rails controller do things (like a Soap request or an insert in a DB)
  • the rails controller update the page and enable the submit button if necessary (in case of errors)

Now I want to add server side code to keeps things really clean if the user bypass the javascript.

Any suggestions?

解决方案

Try using Redis locking and surround your block with something like

Redis.current.lock("#{current_user.id}.action_name") do
   # Some code
end

This is the gem I'm using https://github.com/mlanett/redis-lock

这篇关于在rails控制器,如何防止双提交(当用户双提交提交按钮或命中两次输入)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆