user2755537
user2755537

Reputation: 151

Want to disable submit button if current user has submitted before

So I'm trying to make it so that my submit button disables if you've already submitted the form.

I store your current_user.id when you submit. If you try again you should be met with a submit button that is disabled.

I've been trying to write this with if else statements but it just gets clumsy and doesn't work.

This is what I've tried:

<% if @f.current_user.id.present? %>
 <%= f.submit "Submit" %>
<% else %>
 <p>Disable code here</p>
<% end %>

Upvotes: 0

Views: 456

Answers (1)

Laas
Laas

Reputation: 6058

EDIT: Taking into account the additional info given in the comments the answer is totally rewritten.

First, in the controller you need to check if current_user has posted before:

# In controller action
@user_already_submitted = Answer.where(user_id: current_user.id,
    application_id: @answer.application_id).count > 0

And then in the view:

<%= form_for @answer do |f| %>
  <!-- rest of the form -->
  <%= f.submit "Submit", :disabled => @user_already_submitted %>
<% end %>

Upvotes: 2

Related Questions