Guest User

Untitled

a guest
Jan 16th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. class Issue < ActiveRecord::Base
  2. attr_accessible :body, :end_at, :title
  3. validates_presence_of :title, :body, :end_at
  4. has_many :votes
  5.  
  6. def upvotes_count
  7. votes.count(:conditions => "vote = 1")
  8. end
  9.  
  10. def downvotes_count
  11. votes.count(:conditions => "vote = 0")
  12. end
  13.  
  14. def totalvotes_count
  15. votes.count
  16. end
  17.  
  18. end
  19.  
  20. <% @issues.each do |issue| %>
  21. <li>
  22. <div class="issue">
  23. <h2><%= issue.title %></h2>
  24. <p><%= issue.body %></p>
  25.  
  26. <%= form_for(@vote, :remote => true) do |f| %>
  27. <%= f.hidden_field "issue_id", :value => issue.id %>
  28. <%= f.hidden_field "vote", :value => 1 %>
  29. <%= submit_tag issue.upvotes_count.to_s + " Up", :class => 'up-vote' %>
  30. <% end %>
  31.  
  32. <%= form_for(@vote, :remote => true) do |f| %>
  33. <%= f.hidden_field "issue_id", :value => issue.id %>
  34. <%= f.hidden_field "vote", :value => 0 %>
  35. <%= submit_tag issue.downvotes_count.to_s + " Down", :class => 'down-vote' %>
  36. <% end %>
  37.  
  38. </div>
  39. </li>
  40. <% end %>
  41.  
  42. class VotesController < ApplicationController
  43.  
  44. def index
  45. @votes = Vote.find(:all, :include => :issue)
  46. end
  47.  
  48. def new
  49. @vote = Vote.new(params[:vote])
  50.  
  51. respond_to do |format|
  52. format.html # new.html.erb
  53. format.xml { render :xml => @vote }
  54. end
  55. end
  56.  
  57. def create
  58. @vote = Vote.new(params[:vote])
  59.  
  60. respond_to do |format|
  61. if @vote.save
  62. format.js
  63. format.html { redirect_to issues_path }
  64. else
  65. format.html { redirect_to issues_path }
  66. end
  67. end
  68. end
  69.  
  70. end
  71.  
  72. class IssuesController < ApplicationController
  73. # GET /issues
  74. # GET /issues.json
  75. def index
  76. @issues = Issue.all
  77.  
  78. @vote = Vote.new
  79.  
  80.  
  81. respond_to do |format|
  82. format.html # index.html.erb
  83. format.json { render json: @issues }
  84. end
Add Comment
Please, Sign In to add comment