Skip to content

Commit 2cd8ced

Browse files
committed
the votes
1 parent d11031c commit 2cd8ced

File tree

14 files changed

+74
-8
lines changed

14 files changed

+74
-8
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Place all the behaviors and hooks related to the matching controller here.
2+
# All this logic will automatically be available in application.js.
3+
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

app/assets/stylesheets/votes.css.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place all the styles related to the votes controller here.
2+
// They will automatically be included in application.css.
3+
// You can use Sass (SCSS) here: http://sass-lang.com/

app/controllers/votes_controller.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class VotesController < ApplicationController
2+
def create
3+
topic = Topic.find(params[:topic_id])
4+
vote = topic.votes.build
5+
vote.save!
6+
redirect_to(topics_path)
7+
end
8+
end

app/helpers/votes_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module VotesHelper
2+
end

app/models/topic.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
class Topic < ActiveRecord::Base
22
attr_accessible :description, :title
3+
has_many :votes
34
end

app/models/vote.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Vote < ActiveRecord::Base
2+
attr_accessible :topic_id
3+
belongs_to :topic
4+
end

app/views/topics/index.html.erb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
</tr>
1111

1212
<% @topics.each do |topic| %>
13-
<tr>
14-
<td><%= topic.title %></td>
15-
<td><%= topic.description %></td>
16-
<td><%= link_to 'Show', topic %></td>
17-
<td><%= link_to 'Edit', edit_topic_path(topic) %></td>
18-
<td><%= link_to 'Destroy', topic, method: :delete, data: { confirm: 'Are you sure?' } %></td>
19-
</tr>
13+
<tr>
14+
<td><%= topic.title %></td>
15+
<td><%= topic.description %></td>
16+
<td><%= pluralize(topic.votes.length, "vote") %></td>
17+
<td><%= button_to 'totally!', votes_path(topic_id: topic.id), method: :post %></td>
18+
<td><%= link_to 'Show', topic %></td>
19+
<td><%= link_to 'Edit', edit_topic_path(topic) %></td>
20+
<td><%= link_to 'Destroy', topic, confirm: 'Are you sure?', method: :delete %></td>
21+
</tr>
2022
<% end %>
2123
</table>
2224

config/routes.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
Suggestotron::Application.routes.draw do
2+
resources :votes
3+
4+
25
resources :topics
36

47

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class CreateVotes < ActiveRecord::Migration
2+
def change
3+
create_table :votes do |t|
4+
t.integer :topic_id
5+
6+
t.timestamps
7+
end
8+
end
9+
end

db/schema.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
# It's strongly recommended to check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(:version => 20130309193829) do
14+
ActiveRecord::Schema.define(:version => 20130309211245) do
1515

1616
create_table "topics", :force => true do |t|
1717
t.string "title"
@@ -20,4 +20,10 @@
2020
t.datetime "updated_at", :null => false
2121
end
2222

23+
create_table "votes", :force => true do |t|
24+
t.integer "topic_id"
25+
t.datetime "created_at", :null => false
26+
t.datetime "updated_at", :null => false
27+
end
28+
2329
end

0 commit comments

Comments
 (0)