Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Issue < ActiveRecord::Base
- attr_accessible :body, :end_at, :title
- validates_presence_of :title, :body, :end_at
- has_many :votes
- def upvotes_count
- votes.count(:conditions => "vote = 1")
- end
- def downvotes_count
- votes.count(:conditions => "vote = 0")
- end
- def totalvotes_count
- votes.count
- end
- end
- <% @issues.each do |issue| %>
- <li>
- <div class="issue">
- <h2><%= issue.title %></h2>
- <p><%= issue.body %></p>
- <%= form_for(@vote, :remote => true) do |f| %>
- <%= f.hidden_field "issue_id", :value => issue.id %>
- <%= f.hidden_field "vote", :value => 1 %>
- <%= submit_tag issue.upvotes_count.to_s + " Up", :class => 'up-vote' %>
- <% end %>
- <%= form_for(@vote, :remote => true) do |f| %>
- <%= f.hidden_field "issue_id", :value => issue.id %>
- <%= f.hidden_field "vote", :value => 0 %>
- <%= submit_tag issue.downvotes_count.to_s + " Down", :class => 'down-vote' %>
- <% end %>
- </div>
- </li>
- <% end %>
- class VotesController < ApplicationController
- def index
- @votes = Vote.find(:all, :include => :issue)
- end
- def new
- @vote = Vote.new(params[:vote])
- respond_to do |format|
- format.html # new.html.erb
- format.xml { render :xml => @vote }
- end
- end
- def create
- @vote = Vote.new(params[:vote])
- respond_to do |format|
- if @vote.save
- format.js
- format.html { redirect_to issues_path }
- else
- format.html { redirect_to issues_path }
- end
- end
- end
- end
- class IssuesController < ApplicationController
- # GET /issues
- # GET /issues.json
- def index
- @issues = Issue.all
- @vote = Vote.new
- respond_to do |format|
- format.html # index.html.erb
- format.json { render json: @issues }
- end
Add Comment
Please, Sign In to add comment