0

I am trying to create a voting system using acts_as_votable gem in Rails 4. I have three nominees to be voted from and I want a voter to choose from any of those three and vote only one nominee.acts_as_votable gem allows voter to vote multiple nominees. How do I restrict to only one nominee?

Termininja
  • 6,620
  • 12
  • 48
  • 49

2 Answers2

0

If you keep track of the nominees you can add a before_action in your controller that checks if the current_user has already cast his vote. If so, flash an error and redirect them etc.

bkunzi01
  • 4,504
  • 1
  • 18
  • 25
0

you can write a before_action callback to check if current_user has already voted or not only before the vote action is called. For example

before_action: :check_if_voted, only: :vote

def check_if_voted
  if current_user.voted_for?(@nominee1) || current_user.voted_for?(@nominee2) || current_user.voted_for?(@nominee3)
    flash[:notice] = "You have cast your vote already."
  end
end

And I believe you are using radio button in you form.