Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed @posts in votes_controller issue

  • Loading branch information...
commit 4b8dbcc024ab3c852f85f49b870cd36228d534c2 1 parent b497d25
Apechai authored
4 Gemfile
@@ -5,10 +5,6 @@ gem 'rails', '3.1.1'
5 5
6 6 gem 'sqlite3', '1.3.4', :group => :development
7 7
8   -group :production do
9   - gem "pg"
10   -end
11   -
12 8 group :assets do
13 9 gem 'sass-rails', '~> 3.1.4'
14 10 gem 'coffee-rails', '~> 3.1.1'
235 Gemfile.lock
... ... @@ -0,0 +1,235 @@
  1 +GEM
  2 + remote: http://rubygems.org/
  3 + specs:
  4 + actionmailer (3.1.1)
  5 + actionpack (= 3.1.1)
  6 + mail (~> 2.3.0)
  7 + actionpack (3.1.1)
  8 + activemodel (= 3.1.1)
  9 + activesupport (= 3.1.1)
  10 + builder (~> 3.0.0)
  11 + erubis (~> 2.7.0)
  12 + i18n (~> 0.6)
  13 + rack (~> 1.3.2)
  14 + rack-cache (~> 1.1)
  15 + rack-mount (~> 0.8.2)
  16 + rack-test (~> 0.6.1)
  17 + sprockets (~> 2.0.2)
  18 + activemodel (3.1.1)
  19 + activesupport (= 3.1.1)
  20 + builder (~> 3.0.0)
  21 + i18n (~> 0.6)
  22 + activerecord (3.1.1)
  23 + activemodel (= 3.1.1)
  24 + activesupport (= 3.1.1)
  25 + arel (~> 2.2.1)
  26 + tzinfo (~> 0.3.29)
  27 + activeresource (3.1.1)
  28 + activemodel (= 3.1.1)
  29 + activesupport (= 3.1.1)
  30 + activesupport (3.1.1)
  31 + multi_json (~> 1.0)
  32 + addressable (2.2.6)
  33 + arel (2.2.1)
  34 + bcrypt-ruby (3.0.1)
  35 + builder (3.0.0)
  36 + capybara (1.1.2)
  37 + mime-types (>= 1.16)
  38 + nokogiri (>= 1.3.3)
  39 + rack (>= 1.0.0)
  40 + rack-test (>= 0.5.4)
  41 + selenium-webdriver (~> 2.0)
  42 + xpath (~> 0.1.4)
  43 + childprocess (0.2.2)
  44 + ffi (~> 1.0.6)
  45 + coffee-rails (3.1.1)
  46 + coffee-script (>= 2.2.0)
  47 + railties (~> 3.1.0)
  48 + coffee-script (2.2.0)
  49 + coffee-script-source
  50 + execjs
  51 + coffee-script-source (1.1.3)
  52 + cucumber (1.1.3)
  53 + builder (>= 2.1.2)
  54 + diff-lcs (>= 1.1.2)
  55 + gherkin (~> 2.6.7)
  56 + json (>= 1.4.6)
  57 + term-ansicolor (>= 1.0.6)
  58 + cucumber-rails (1.2.0)
  59 + capybara (>= 1.1.1)
  60 + cucumber (>= 1.1.1)
  61 + nokogiri (>= 1.5.0)
  62 + database_cleaner (0.7.0)
  63 + devise (1.5.1)
  64 + bcrypt-ruby (~> 3.0)
  65 + orm_adapter (~> 0.0.3)
  66 + warden (~> 1.1)
  67 + diff-lcs (1.1.3)
  68 + em-websocket (0.3.5)
  69 + addressable (>= 2.1.1)
  70 + eventmachine (>= 0.12.9)
  71 + erubis (2.7.0)
  72 + eventmachine (0.12.10)
  73 + execjs (1.2.9)
  74 + multi_json (~> 1.0)
  75 + factory_girl (2.3.0)
  76 + activesupport
  77 + factory_girl_rails (1.4.0)
  78 + factory_girl (~> 2.3.0)
  79 + railties (>= 3.0.0)
  80 + ffi (1.0.11)
  81 + gherkin (2.6.8)
  82 + json (>= 1.4.6)
  83 + growl (1.0.3)
  84 + guard (0.8.8)
  85 + thor (~> 0.14.6)
  86 + guard-bundler (0.1.3)
  87 + bundler (>= 1.0.0)
  88 + guard (>= 0.2.2)
  89 + guard-cucumber (0.7.4)
  90 + cucumber (>= 0.10)
  91 + guard (>= 0.8.3)
  92 + guard-livereload (0.3.1)
  93 + em-websocket (>= 0.2.0)
  94 + guard (>= 0.4.0)
  95 + multi_json (~> 1.0.3)
  96 + guard-rails (0.0.3)
  97 + guard (>= 0.2.2)
  98 + guard-rspec (0.5.5)
  99 + guard (>= 0.8.4)
  100 + hike (1.2.1)
  101 + i18n (0.6.0)
  102 + jquery-rails (1.0.18)
  103 + railties (~> 3.0)
  104 + thor (~> 0.14)
  105 + json (1.6.1)
  106 + json_pure (1.6.1)
  107 + launchy (2.0.5)
  108 + addressable (~> 2.2.6)
  109 + less (2.0.7)
  110 + therubyracer (~> 0.9.2)
  111 + less-rails (2.1.0)
  112 + actionpack (~> 3.1.1)
  113 + less (~> 2.0.7)
  114 + libv8 (3.3.10.4)
  115 + mail (2.3.0)
  116 + i18n (>= 0.4.0)
  117 + mime-types (~> 1.16)
  118 + treetop (~> 1.4.8)
  119 + mime-types (1.17.2)
  120 + multi_json (1.0.3)
  121 + nokogiri (1.5.0)
  122 + orm_adapter (0.0.5)
  123 + polyglot (0.3.3)
  124 + rack (1.3.5)
  125 + rack-cache (1.1)
  126 + rack (>= 0.4)
  127 + rack-mount (0.8.3)
  128 + rack (>= 1.0.0)
  129 + rack-ssl (1.3.2)
  130 + rack
  131 + rack-test (0.6.1)
  132 + rack (>= 1.0)
  133 + rails (3.1.1)
  134 + actionmailer (= 3.1.1)
  135 + actionpack (= 3.1.1)
  136 + activerecord (= 3.1.1)
  137 + activeresource (= 3.1.1)
  138 + activesupport (= 3.1.1)
  139 + bundler (~> 1.0)
  140 + railties (= 3.1.1)
  141 + rails-footnotes (3.7.5)
  142 + rails (>= 3.0.0)
  143 + railties (3.1.1)
  144 + actionpack (= 3.1.1)
  145 + activesupport (= 3.1.1)
  146 + rack-ssl (~> 1.3.2)
  147 + rake (>= 0.8.7)
  148 + rdoc (~> 3.4)
  149 + thor (~> 0.14.6)
  150 + rake (0.9.2.2)
  151 + rb-fsevent (0.4.3.1)
  152 + rdoc (3.11)
  153 + json (~> 1.4)
  154 + rspec (2.8.0.rc1)
  155 + rspec-core (= 2.8.0.rc1)
  156 + rspec-expectations (= 2.8.0.rc1)
  157 + rspec-mocks (= 2.8.0.rc1)
  158 + rspec-core (2.8.0.rc1)
  159 + rspec-expectations (2.8.0.rc1)
  160 + diff-lcs (~> 1.1.2)
  161 + rspec-mocks (2.8.0.rc1)
  162 + rspec-rails (2.8.0.rc1)
  163 + actionpack (~> 3.0)
  164 + activesupport (~> 3.0)
  165 + railties (~> 3.0)
  166 + rspec (= 2.8.0.rc1)
  167 + rubyzip (0.9.4)
  168 + sass (3.1.10)
  169 + sass-rails (3.1.5)
  170 + actionpack (~> 3.1.0)
  171 + railties (~> 3.1.0)
  172 + sass (~> 3.1.10)
  173 + tilt (~> 1.3.2)
  174 + selenium-webdriver (2.13.0)
  175 + childprocess (>= 0.2.1)
  176 + ffi (~> 1.0.9)
  177 + json_pure
  178 + rubyzip
  179 + sprockets (2.0.3)
  180 + hike (~> 1.2)
  181 + rack (~> 1.0)
  182 + tilt (~> 1.1, != 1.3.0)
  183 + sqlite3 (1.3.4)
  184 + term-ansicolor (1.0.7)
  185 + therubyracer (0.9.9)
  186 + libv8 (~> 3.3.10)
  187 + thor (0.14.6)
  188 + tilt (1.3.3)
  189 + treetop (1.4.10)
  190 + polyglot
  191 + polyglot (>= 0.3.1)
  192 + twitter-bootstrap-rails (1.4.1)
  193 + actionpack
  194 + jquery-rails (~> 1.0)
  195 + less
  196 + less-rails (~> 2.1.0)
  197 + railties
  198 + tzinfo (0.3.31)
  199 + uglifier (1.1.0)
  200 + execjs (>= 0.3.0)
  201 + multi_json (>= 1.0.2)
  202 + warden (1.1.0)
  203 + rack (>= 1.0)
  204 + will_paginate (3.0.2)
  205 + xpath (0.1.4)
  206 + nokogiri (~> 1.3)
  207 +
  208 +PLATFORMS
  209 + ruby
  210 +
  211 +DEPENDENCIES
  212 + capybara (>= 1.1.1)
  213 + coffee-rails (~> 3.1.1)
  214 + cucumber-rails (>= 1.2.0)
  215 + database_cleaner (>= 0.6.7)
  216 + devise (>= 1.4.9)
  217 + factory_girl_rails (>= 1.3.0)
  218 + growl
  219 + guard (>= 0.6.2)
  220 + guard-bundler (>= 0.1.3)
  221 + guard-cucumber (>= 0.6.1)
  222 + guard-livereload (>= 0.3.0)
  223 + guard-rails (>= 0.0.3)
  224 + guard-rspec (>= 0.4.3)
  225 + jquery-rails
  226 + launchy (>= 2.0.5)
  227 + rails (= 3.1.1)
  228 + rails-footnotes (>= 3.7)
  229 + rb-fsevent
  230 + rspec-rails (>= 2.8.0.rc1)
  231 + sass-rails (~> 3.1.4)
  232 + sqlite3 (= 1.3.4)
  233 + twitter-bootstrap-rails
  234 + uglifier (>= 1.0.3)
  235 + will_paginate
3  app/controllers/posts_controller.rb
@@ -28,6 +28,9 @@ def create
28 28 end
29 29
30 30 def destroy
  31 + @post = Post.find(params[:id])
  32 + @post.destroy
  33 + redirect_to root_path
31 34 end
32 35
33 36 end
1  app/controllers/users_controller.rb
@@ -2,6 +2,7 @@ class UsersController < ApplicationController
2 2 before_filter :authenticate_user!
3 3
4 4 def show
  5 + @vote = Vote.new(params[:vote])
5 6 @user = User.find(params[:id])
6 7 @posts = @user.posts.paginate(:page => params[:page])
7 8 @post = current_user.posts.build(params[:post])
1  app/controllers/votes_controller.rb
@@ -20,6 +20,7 @@ def create
20 20 redirect_to root_path
21 21 else
22 22 flash[:failure] = "You did not vote"
  23 + @posts = Post.popular
23 24 render 'posts/index'
24 25 end
25 26
8 app/models/vote.rb
... ... @@ -1,6 +1,14 @@
1 1 class Vote < ActiveRecord::Base
2 2 attr_accessible :user_id, :post_id
  3 + validate :only_one_user_per_post
3 4
4 5 belongs_to :user
5 6 belongs_to :post, :counter_cache => true
  7 +
  8 +private
  9 + def only_one_user_per_post
  10 + if Vote.where("user_id = ? AND post_id = ?", self.user_id, self.post_id).all.any?
  11 + errors.add(:user_id, "Can only post once per post!")
  12 + end
  13 + end
6 14 end
2  app/views/layouts/application.html.erb
@@ -20,7 +20,7 @@
20 20 <div class="topbar">
21 21 <div class="fill">
22 22 <div class="container">
23   - <h3><a href="#">Learning Quotient</a></h3>
  23 + <h3><%= link_to "Learning Quotient", posts_path %></h3>
24 24 <ul>
25 25 <%= render 'shared/navigation' %>
26 26 </ul>
9 app/views/posts/_post.html.erb
@@ -10,7 +10,14 @@
10 10 <h3><%= link_to post.title, post.link %></h3>
11 11 <% end %>
12 12 <p><%= post.content %></p>
13   - <small><%= post.user.name %></small>
  13 + <small><%= link_to post.user.name, post.user %></small>
  14 + <% if user_signed_in? %>
  15 + <% if current_user == @user %>
  16 + <%= link_to "delete", post, :method => :delete,
  17 + :confirm => "You sure?",
  18 + :class => "danger btn"%>
  19 + <% end %>
  20 + <% end %>
14 21 </div>
15 22 <%= f.hidden_field "post_id", :value => post.id %>
16 23 <%= f.hidden_field "user_id", :value => post.user.id %>
4 app/views/posts/index.html.erb
@@ -20,6 +20,10 @@
20 20 <li><%= link_to "New", :controller => "posts", :action => "new" %></li>
21 21 </ul>
22 22
  23 +<% flash.each do |key, value| %>
  24 + <div class="flash <%= key %>"><%= value %></div>
  25 +<% end %>
  26 +
23 27 <table class="posts" summary="User posts">
24 28 <%= render @posts %>
25 29 </table>
4 app/views/users/show.html.erb
@@ -3,8 +3,6 @@
3 3 <table class="posts" summary="User posts">
4 4 <%= render @posts %>
5 5 </table>
6   - <%= will_paginate @posts %>
7 6 <% end %>
8   - Write a new post:
9   - <%= render 'shared/post_form' %>
  7 +
10 8

0 comments on commit 4b8dbcc

Please sign in to comment.
Something went wrong with that request. Please try again.