Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6658909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:58:06+00:00 2026-05-26T01:58:06+00:00

I would like for visitors to only be able to vote on each link

  • 0

I would like for visitors to only be able to vote on each link once. I understand that if they clear their cookies, they’ll be able to vote again. How would I implement this feature in rails? I’ve attached my files below. Thanks

Controller https://gist.github.com/1274274

View https://gist.github.com/1274275

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:58:07+00:00Added an answer on May 26, 2026 at 1:58 am

    I would not recommend doing it that way but if you insist I would say you should create a hash with the ids of the link as keys and their values set to true or false according to the type of vote.

    I would change your controller like this:

    def up
        @link = Link.find(params[:id])
    
        session[:voting] ||= {}
    
        unless session[:voting][@link.id]
            if session[:voting][@link.id].nil?
                points = 1
            else
                points = 2
    
            @link.update_attribute :points, @link.points + points
    
            session[:voting][@link.id] = true
        end
    
        redirect_to :action => :index 
    end
    
    def down 
        @link = Link.find(params[:id])
    
        session[:voting] ||= {}
    
        unless session[:voting][@link.id] == false
            if session[:voting][@link.id]
                points = 2
            else
                points = 1
    
            @link.update_attribute :points, @link.points - points
    
            session[:voting][@link.id] = false
        end
    
        redirect_to :action => :index 
    end
    

    This logic should belong to the model but we shouldn’t access the session variable inside the model. You can refactor this code inside the model and just pass the session to it from the controller. It would be something like this:

    def vote type, session
        return if session[:voting][id] == type
    
        added_points = session[:voting][id].nil? ? 1 : 2
    
        update_attribute :points, points + (type ? added_points: -added_points)
    
        session[:voting][id] = type
    end
    

    Find more about sessions here:
    http://guides.rubyonrails.org/action_controller_overview.html#session

    Hope that works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My client would like website visitors to embed videos in their profiles. We've suggested
I would like to create a widget so that my visitors can display it
I would like to enable the visitors of my website to login using their
I would like to make a website that is php and only requires one
I would like to redirect visitors to a login page with added parameters (based
I would like to be able to detect what country a visitor is from
Would like to be able to set colors of headings and such, different font
Would like to make anapplication in Java that will not automatically parse parameters used
My visitors get a cached version of the page from Varnish. I would like
I would like to make an HTML text input field that always shows its

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.