Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 2 commits
  • 10 files changed
  • 0 commit comments
  • 1 contributor
View
7 app/controllers/votes_controller.rb
@@ -0,0 +1,7 @@
+class VotesController < ApplicationController
+ def create
+ @post = Post.find(params[:post_id])
+ @vote = @post.votes.create(params[:vote])
+ redirect_to posts_path
+ end
+end
View
4 app/models/post.rb
@@ -1,4 +1,8 @@
class Post < ActiveRecord::Base
has_many :comments
+ has_many :votes
attr_accessible :title, :url
+ def votes_count
+ votes.where(:upvote => true).count - votes.where(:upvote => false).count
+ end
end
View
4 app/models/vote.rb
@@ -0,0 +1,4 @@
+class Vote < ActiveRecord::Base
+ belongs_to :post
+ attr_accessible :upvote
+end
View
18 app/views/posts/index.html.erb
@@ -1,7 +1,23 @@
<h2>Post list</h2>
<% @posts.each do |post| %>
<p> <%= link_to post.title, post.url %>
- <%= link_to pluralize(post.comments.count, ' comment'), post %> </p>
+ <%= link_to pluralize(post.comments.count, ' comment'), post %>
+ <%= pluralize(post.votes_count, ' vote' )%>
+ <% link_to "upvote failed", post_votes_path(post), :data => {:vote => "vote[:upvote => :true]"},:method => :post %>
+ <% link_to "upvote failed2", post_votes_path(post), :vote => [:upvote =>true],:method => :post %>
+ <%= link_to "upvote", post_votes_path(post, :vote => [:upvote =>true]), :method => :post %>
+ <%= link_to "downvote", post_votes_path(post, :vote => [:upvote =>false]), :method => :post %>
+
+ <%= form_for post.votes.new, :url => post_votes_path(post) do |f| %>
+ <%= f.hidden_field :upvote, { :value => true } %>
+ <%= f.submit "Upvote" %>
+<% end %>
+<%= form_for post.votes.new, :url => post_votes_path(post) do |f| %>
+ <%= f.hidden_field :upvote, { :value => false } %>
+ <%= f.submit "Downvote" %>
+<% end %>
+ </p>
+
<% end %>
<%= link_to "Submit a Link", new_post_path %>
View
16 app/views/posts/new.html.erb
@@ -1,6 +1,7 @@
<h2>Add new Post</h2>
+<h3> Form based on form_for </h3>
<%= form_for (@post) do |f| %>
- <div class="field">
+ <div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
@@ -12,3 +13,16 @@
<%= f.submit %>
</div>
<% end %>
+
+<h3> Form based on form_tag </h3>
+<%= form_tag(@post) do -%>
+ <div class="field">
+ <%= label_tag 'title' %><br />
+ <%= text_field_tag 'post[title]' %><br />
+ </div>
+ <div class="field">
+ <%= label_tag 'url' %><br />
+ <%= text_field_tag 'post[url]' %><br />
+ </div>
+ <div><%= submit_tag 'Create post' %></div>
+<% end -%>
View
1 config/routes.rb
@@ -3,5 +3,6 @@
root to: "posts#index"
resources :posts, :only => [:index, :show, :new, :create] do
resources :comments, :only => :create
+ resources :votes, :only => :create
end
end
View
11 db/migrate/20121206142713_create_votes.rb
@@ -0,0 +1,11 @@
+class CreateVotes < ActiveRecord::Migration
+ def change
+ create_table :votes do |t|
+ t.boolean :upvote
+ t.references :post
+
+ t.timestamps
+ end
+ add_index :votes, [:post_id, :upvote]
+ end
+end
View
11 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20121128135451) do
+ActiveRecord::Schema.define(:version => 20121206142713) do
create_table "comments", :force => true do |t|
t.text "content"
@@ -29,4 +29,13 @@
t.datetime "updated_at", :null => false
end
+ create_table "votes", :force => true do |t|
+ t.boolean "upvote"
+ t.integer "post_id"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ add_index "votes", ["post_id", "upvote"], :name => "index_votes_on_post_id_and_upvote"
+
end
View
9 test/fixtures/votes.yml
@@ -0,0 +1,9 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+
+one:
+ upvote:
+ post:
+
+two:
+ upvote:
+ post:
View
7 test/unit/vote_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class VoteTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end

No commit comments for this range

Something went wrong with that request. Please try again.