Skip to content

Instantly share code, notes, and snippets.

@courtneyphillips
Last active March 15, 2016 17:19
Show Gist options
  • Save courtneyphillips/7d51b5af4534d6f33e9d to your computer and use it in GitHub Desktop.
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.
def upvote
@item = Item.find(params[:id])
@item.upvotes += 1
@item.save
redirect_to items_path
end
Rails.application.routes.draw do
resources :items do
member do
post :upvote
post :downvote
end
end
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
<%= 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