0

It's probably trivial question but i can't find any solutions to make this right.

def make_thing
  @thing = Thing.new

  if @thing.save
    redirect_to some_path
  end
end

<%= link_to "Make Thing", controller: :things, action: :make_thing %>

Sending multiple requests(by pressing "Make Thing" link) to "make_thing" action will multiple call save method before it would be redirected. So this will create multiple records. How can i record only single "thing" and redirect then?

1 Answers1

0

You should use disable_with (Rails 4: Disable submit button after click) and check uniqueness of the record at model- or database-level.

UPD:

You can do something like this for disabling with image (slim):

= link_to "#", data: { disable_with: "<span class='with_image' class='with_image'>Submitting...</span>" } span | Submit

And css:

.with_image { background: url('/assets/test.jpg'); }

But it is better to use jquery loader or something like that... Hope I helped with my answer.

Community
  • 1
  • 1
Vitaly
  • 161
  • 1
  • 4