Alex Wayne
Alex Wayne

Reputation: 187154

Preventing form submission conflicts between multiple users (Rails 2.x)

Let's say I have a simple form in the administration of my Rails 2.x powered website.

<form action="/products/123">

  Price:
  <input type="text" name="product[price]" value="12.99"></input>

  Description:
  <textarea name="product[description]">
    A long and descriptive block of text goes here.
  </textarea>

  <input type="submit"></input>
</form>
  1. Admin Sally wants to change the description.
  2. Admin Sally opens the page with this form and begins writing some new sales copy in the textarea.
  3. A few seconds later, admin Joe decides this product needs to go on sale
  4. Admin Joe opens the page with the form on it
  5. Admin Joe lowers the price to $9.99
  6. Admin Joe submits the form, setting the price in the database to $9.99
  7. Admin Sally is done writing the copy and submits the form, but the price field in her browser still says $12.99.
  8. Joe becomes angry and yells at Sally for changing the price back, even though she had no idea she just did that.

So the database is updated to contain what Sally had in the form, removing the price that Joe set while she was working with that page open.

Surely I am not the first one to stumble on a problem like this, but I've never had to deal with it. So first of all, does this sort of problem have a name? And second, what are some solutions to making it suck less?


A few solutions that come to mind, but some have serious drawbacks.

But again, I'm sure I'm not the first person to have this problem. So is there a standard solution for this?

Upvotes: 0

Views: 410

Answers (1)

Victor Moroz
Victor Moroz

Reputation: 9225

Have you checked this: http://api.rubyonrails.org/classes/ActiveRecord/Locking/Optimistic.html ?

UPDATE

Well, it doesn't really explain what to do if lock_version is wrong, but at least it's better than updated_at I guess. It all depends on who wins and why. If Sally didn't touch price it seems reasonable to update price from Joe's data when saving. Just add original values as hidden fields and compare them with Sally's input when saving in case lock_version is wrong. If they both modified same field, well, no luck. Somebody should win, or you can go back to the edit page and highlight changes for acceptance.

Upvotes: 2

Related Questions