SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question [simply rails 2] restricting multiple "diggs" from the same user?

    what is the best way to restirct users, so they can cast only one vote per submission?
    I've tried few things, but none of them work. only thing that does work is this line in create.js.rjs:
    Code Ruby:
    page.replace_html 'vote_form', "voted"
    but as soon as the page is reloaded, user can vote again.

    then I've tried editing the controller, so that it checks if user already voted, but I got stuck.


    help?
    thx

  2. #2
    Non-Member
    Join Date
    Jul 2008
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think it needs to make a record in the cookies...

  3. #3
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking

    Quote Originally Posted by itseasyaspie View Post
    i think it needs to make a record in the cookies...
    ok, but how

  4. #4
    Non-Member
    Join Date
    Jul 2008
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is the script you are using cookie based?it should say if it is, where did you down load it from?

  5. #5
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by itseasyaspie View Post
    is the script you are using cookie based?it should say if it is, where did you down load it from?
    I followed 9 chapters in the Simply Rails 2 by Patrick Lenz if that is what you mean. I know cookies are used, but I'm not sure if that means that the script is cookie based

    Thanks in advance.

  6. #6
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Once you get past p. 322 in the book you will start to store the user_id of the voter with each vote. Once you have this you could easily enough search the votes table for all past votes of the user to see if they have an existing vote for the story based on user_id and story_id.

    For example you can try something like this:

    v = Vote.find_all_by_user_id_and_story_id(1,1)


    This should get you all votes for a given user_id and story_id ... if you get an empty array (v.size == 0) you know the person has not voted for this before .... if you get any results at all (v.size > 0) they voted at least once.

    This idea is easy enough to test in the Rails Console. Once you have the correct query you can just add some code in the Votes Controller 'create' method to determine if you should add a new vote or you could remove the vote button from the story page to stop them from attempting to vote. Or both for safety ;-)


    ...and don't EVER try to do this with cookies ... even if you persist the cookie a user can purge the cookies from their browser manually so you can never be sure a missing cookie means someone has/has not done something.

  7. #7
    SitePoint Member
    Join Date
    Jul 2008
    Location
    UK
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did something like this, but decided to just filter by IP address, so once some at IP address 10.0.0.31 had voted, no-one else could vote from that IP address for the next 24 hours.

  8. #8
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mfromin View Post
    Once you get past p. 322 in the book you will start to store the user_id of the voter with each vote. Once you have this you could easily enough search the votes table for all past votes of the user to see if they have an existing vote for the story based on user_id and story_id.

    For example you can try something like this:

    v = Vote.find_all_by_user_id_and_story_id(1,1)


    This should get you all votes for a given user_id and story_id ... if you get an empty array (v.size == 0) you know the person has not voted for this before .... if you get any results at all (v.size > 0) they voted at least once.

    This idea is easy enough to test in the Rails Console. Once you have the correct query you can just add some code in the Votes Controller 'create' method to determine if you should add a new vote or you could remove the vote button from the story page to stop them from attempting to vote. Or both for safety ;-)


    ...and don't EVER try to do this with cookies ... even if you persist the cookie a user can purge the cookies from their browser manually so you can never be sure a missing cookie means someone has/has not done something.
    This seems to be the best way to limit the voting. I don't think limiting the votes by IP address really helps solve the problem.

    How would it be possible to modify the votes controller create action to prevent a user from voting on a story more than once?

  9. #9
    SitePoint Enthusiast dnectom's Avatar
    Join Date
    Dec 2005
    Location
    Lincolnshire, Uk
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I add the following code to the stories_controller.rb
    Code:
      
      def show
      	v = Vote.find_all_by_user_id_and_story_id(session[:user_id],params[:id].to_i)
      	@voted = !v.empty?
      	@story = Story.find(params[:id])  	
      end
    Then simply modified the show.html.erb file to do a check whether @voted is set or not and either display the vote form.. or an already voted element..


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •