Rails: Prevent duplicate inserts due to pressing back button and save again

前端 未结 8 1355
难免孤独
难免孤独 2020-12-09 12:13

Think about a simple Rails scaffold application with a \"new\" action containing a form to add records to a database with a \"save\" button. After the \"create\" action the

8条回答
  •  南笙
    南笙 (楼主)
    2020-12-09 13:05

    You can use validators to make sure that no duplicate values are inserted. In this case validates_uniqueness_of :field

    If you for example want to prevent users from having the same email address you could put the following code in your user model.

    validates_uniqueness_of :email
    

    This checks the column for any previous entries that are the same as the one your trying to inert. Good luck

提交回复
热议问题