Last active
          March 15, 2016 17:19 
        
      - 
      
- 
        Save courtneyphillips/7d51b5af4534d6f33e9d to your computer and use it in GitHub Desktop. 
    Example controller, view, schema and routes for including upvote/downvote functionality in Ruby on Rails.
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | def upvote | |
| @item = Item.find(params[:id]) | |
| @item.upvotes += 1 | |
| @item.save | |
| redirect_to items_path | |
| end | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | Rails.application.routes.draw do | |
| resources :items do | |
| member do | |
| post :upvote | |
| post :downvote | |
| end | |
| end | |
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | create_table "items", force: :cascade do |t| | |
| t.string "name" | |
| t.string "author" | |
| t.integer "upvotes", default: 0 | |
| t.integer "downvotes", default: 0 | |
| t.datetime "created_at" | |
| t.datetime "updated_at" | |
| end | |
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <%= link_to '▲', upvote_item_path(item), :data => {:method => "update"} %> | |
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment