简体   繁体   中英

Ruby on Rails acts_as_votable gem

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM