From e718f58ec34bf67081481f297c31345ca8c5e73c Mon Sep 17 00:00:00 2001 From: kristallam Date: Fri, 13 May 2016 15:22:36 -0700 Subject: [PATCH 1/8] Change method for vote sum --- app/models/question.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/question.rb b/app/models/question.rb index a64409b..9645a16 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -7,7 +7,7 @@ class Question < ActiveRecord::Base validates :title, :text, presence: true def up_vote_sum - votes.sum(:up_vote) + votes.sum("1 + :up_vote") end def down_vote_sum From ef2c9379580fdf92c3e181ef84aca6e0fd6024d5 Mon Sep 17 00:00:00 2001 From: kristallam Date: Fri, 13 May 2016 16:05:30 -0700 Subject: [PATCH 2/8] Change controller --- app/controllers/questions_controller.rb | 6 +++++- app/models/question.rb | 14 ++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index c516bb3..d58fd25 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -39,6 +39,7 @@ #ANSWER A QUESTION ROUTES get '/questions/:id/answers/new' do + end post '/questions/:id/answers' do @@ -53,6 +54,7 @@ #RESPOND TO A QUESTION'S ANSWER ROUTES get '/questions/:question_id/answers/:id/responses/new' do + end post '/questions/:question_id/answers/:id/responses' do @@ -68,11 +70,13 @@ post '/questions/:id/vote' do p params @question = Question.find_by(id: params[:id]) - @vote = Vote.new + @vote = Vote.create(user_id: @question.user_id, votable_id: @question.id) if request.xhr? if params[:data] == "upvote" @vote.up_vote = 1 @vote.save + p "=======" * 50 + p @question.up_vote_sum return @question.up_vote_sum.to_s elsif params[:data] == "downvote" @vote.down_vote = 1 diff --git a/app/models/question.rb b/app/models/question.rb index 281363a..ff49213 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -8,10 +8,20 @@ class Question < ActiveRecord::Base validates :title, :text, presence: true def up_vote_sum - votes.sum("1 + :up_vote") + sum = 0 + p "***" * 50 + p self.votes + self.votes.each do |vote| + if vote.up_vote == 1 + sum += 1 + end + end + return sum + # self.votes.sum(:up_vote) + # p self.votes end def down_vote_sum - votes.sum(:down_vote) + self.votes.sum(:down_vote) end end From e1820959e0998661a7747872c970e6a078a4a4f3 Mon Sep 17 00:00:00 2001 From: kristallam Date: Fri, 13 May 2016 16:10:25 -0700 Subject: [PATCH 3/8] Working vote up and down buttons --- app/controllers/questions_controller.rb | 7 ++----- app/models/question.rb | 12 +++++++----- public/js/application.js | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index d58fd25..fd1185c 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -68,15 +68,12 @@ end post '/questions/:id/vote' do - p params @question = Question.find_by(id: params[:id]) - @vote = Vote.create(user_id: @question.user_id, votable_id: @question.id) + @vote = Vote.create(user_id: @question.user_id, votable_id: @question.id, votable_type: "Question") if request.xhr? if params[:data] == "upvote" @vote.up_vote = 1 @vote.save - p "=======" * 50 - p @question.up_vote_sum return @question.up_vote_sum.to_s elsif params[:data] == "downvote" @vote.down_vote = 1 @@ -84,6 +81,6 @@ return @question.down_vote_sum.to_s end else - # redirect "/questions/#{params[:id]}/vote" + redirect "/questions/#{params[:id]}/vote" end end diff --git a/app/models/question.rb b/app/models/question.rb index ff49213..7dda4b9 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -9,19 +9,21 @@ class Question < ActiveRecord::Base def up_vote_sum sum = 0 - p "***" * 50 - p self.votes self.votes.each do |vote| if vote.up_vote == 1 sum += 1 end end return sum - # self.votes.sum(:up_vote) - # p self.votes end def down_vote_sum - self.votes.sum(:down_vote) + sum = 0 + self.votes.each do |vote| + if vote.down_vote == 1 + sum += 1 + end + end + return sum end end diff --git a/public/js/application.js b/public/js/application.js index 43b6b87..f5e1c47 100644 --- a/public/js/application.js +++ b/public/js/application.js @@ -29,4 +29,18 @@ $(document).ready(function() { $("#upvote-button").html(result); }) }) + + $("#downvote-button").on('click', function(event){ + event.preventDefault(); + console.log(this) + var url = $(this).parent().attr('action'); + $.ajax({ + url: url, + method: 'POST', + data: "data=downvote" + }).done(function(result){ + console.log(result); + $("#downvote-button").html(result); + }) + }) }); From 6fff7d9e017ba62efd227c0f7cb8407627fe80d2 Mon Sep 17 00:00:00 2001 From: Bill Date: Fri, 13 May 2016 16:30:52 -0700 Subject: [PATCH 4/8] Add new answers feature --- app/controllers/questions_controller.rb | 11 ++++++++++ app/views/answers/_new.erb | 1 + app/views/answers/new.erb | 2 +- app/views/questions/show.erb | 21 ++++++++++++------- public/js/application.js | 28 +++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 9 deletions(-) diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index c516bb3..dbdfb57 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -16,6 +16,15 @@ end end +post '/questions/:question_id/answers' do + @answer = Answer.create(params[:answer]) + @question = Question.find(params[:question_id]) + @question.answers << @answer + if request.xhr? + erb :'answers/_new', layout: false + end +end + get '/questions/:id' do @question = Question.find_by(id: params[:id]) @answers = @question.answers @@ -39,6 +48,8 @@ #ANSWER A QUESTION ROUTES get '/questions/:id/answers/new' do + @question = Question.find(params[:id]) + erb :'answers/new', layout: false end post '/questions/:id/answers' do diff --git a/app/views/answers/_new.erb b/app/views/answers/_new.erb index e69de29..fea10f0 100644 --- a/app/views/answers/_new.erb +++ b/app/views/answers/_new.erb @@ -0,0 +1 @@ +
Answers:
<%= @answer.text %>
diff --git a/app/views/answers/new.erb b/app/views/answers/new.erb index af5116d..6fec42f 100644 --- a/app/views/answers/new.erb +++ b/app/views/answers/new.erb @@ -1,4 +1,4 @@ -
+
Your answer here Content:
diff --git a/app/views/questions/show.erb b/app/views/questions/show.erb index 5b6cb6c..b4a8807 100644 --- a/app/views/questions/show.erb +++ b/app/views/questions/show.erb @@ -1,25 +1,30 @@

<%= @question.title %>

-
+
-
-
<%= @question.text %>
+
Description:
<%= @question.text %>
<% @question.responses.each do |response| %> -

<%= response %>

+

<%= response.text %>

<% end %> -
-
+ + +
+
+ +
+Answers: <% @answers.each do |answer| %> -
<%= answer %>


<%= answer.text %> <% answer.responses.each do |response| %> -

<%= response %>

+

<%= response.text %>

<% end %> <% end %>
+
diff --git a/public/js/application.js b/public/js/application.js index 43b6b87..86e4632 100644 --- a/public/js/application.js +++ b/public/js/application.js @@ -29,4 +29,32 @@ $(document).ready(function() { $("#upvote-button").html(result); }) }) + + $("#answer-form-show").on('click', function(event){ + event.preventDefault(); + var url = $(this).attr("action"); + $.ajax({ + method: "get", + url: url, + }).done(function(response){ + $("#answers-list").prepend(response); + }) + }) + + $("#question-show-page").on("submit", "#answer-form", function(ev){ + ev.preventDefault(); + var data = $("#answer-form").serialize(); + var url = $("#answer-form").attr("action"); + $.ajax({ + url: url, + method: 'post', + data: data + }).done(function(answer) { + $("#answers-list").append(answer); + $("#answer-form").remove(); + + }) + }); + + }); From 46968c53389e9c4d79902767a7083e5d5efb35de Mon Sep 17 00:00:00 2001 From: Bill Date: Fri, 13 May 2016 16:34:39 -0700 Subject: [PATCH 5/8] Remove
--- app/views/answers/_new.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/answers/_new.erb b/app/views/answers/_new.erb index fea10f0..f4b7514 100644 --- a/app/views/answers/_new.erb +++ b/app/views/answers/_new.erb @@ -1 +1 @@ -
Answers:
<%= @answer.text %>
+

<%= @answer.text %>
From 1215658739af30d6bd1df225c6383246abbcf638 Mon Sep 17 00:00:00 2001 From: Riley Scheid Date: Fri, 13 May 2016 16:35:47 -0700 Subject: [PATCH 6/8] Update merge --- app/controllers/questions_controller.rb | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index fd1185c..54f1e7f 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -23,45 +23,26 @@ end -#RESPOND TO A QUESTION ROUTES -get '/questions/:id/responses/new' do -end - post '/questions/:id/responses' do - @response = Response.new() + @response = Response.new(user_id: session[:user_id], respondable_id: params[:id], respondable_type: "Question") @question = Question.find_by(id: params[:id]) @question.responses << @response - # @question.save DO WE NEED THIS? @response.save redirect "/questions/#{@question.id}" end - -#ANSWER A QUESTION ROUTES -get '/questions/:id/answers/new' do - -end - post '/questions/:id/answers' do - @answer = Answer.new() + @answer = Answer.new(user_id: session[:user_id], question_id: params[:id]) @question = Question.find_by(id: params[:id]) @question.answers << @answer - # @question.save DO WE NEED THIS? @answer.save redirect "/questions/#{@question.id}" end - -#RESPOND TO A QUESTION'S ANSWER ROUTES -get '/questions/:question_id/answers/:id/responses/new' do - -end - post '/questions/:question_id/answers/:id/responses' do - @response = Response.new() + @response = Response.new(user_id: session[:user_id], respondable_id: params[:id], respondable_type: "Answer") @answer = Answer.find_by(id: params[:id]) @answer.responses << @response - # @answer.save DO WE NEED THIS? @response.save @question = Question.find_by(id: params[:question_id]) redirect "/questions/#{@question.id}" From 1ee85d00e827852fb854e9cc4f9551a322c68884 Mon Sep 17 00:00:00 2001 From: Riley Scheid Date: Fri, 13 May 2016 16:51:34 -0700 Subject: [PATCH 7/8] Something? --- app/controllers/index_controller.rb | 3 +- app/controllers/users.rb | 48 ++++++++++++++--------------- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/app/controllers/index_controller.rb b/app/controllers/index_controller.rb index 99ab901..0caf83e 100644 --- a/app/controllers/index_controller.rb +++ b/app/controllers/index_controller.rb @@ -3,7 +3,6 @@ end get '/' do - @questions = Question.all.reverse erb :index end @@ -25,7 +24,9 @@ end post '/users' do + p params['user'] @user = User.new(params['user']) + p @user if @user.save login(@user) redirect '/' diff --git a/app/controllers/users.rb b/app/controllers/users.rb index 1addf0f..991f36b 100644 --- a/app/controllers/users.rb +++ b/app/controllers/users.rb @@ -1,28 +1,28 @@ -get '/' do - erb :'index' -end +# get '/' do +# erb :'index' +# end -get '/users/new' do +# get '/users/new' do - erb :'/users/new' -end +# erb :'/users/new' +# end -post '/users' do - user = User.new(params[:user_info]) - if user.save - session[:user_id] = user.id - redirect "/users/#{user.id}" - else - redirect '/users/new' - end -end +# # post '/users' do +# # user = User.new(params['user']) +# # if user.save +# # session[:user_id] = user.id +# # redirect "/users/#{user.id}" +# # else +# # redirect '/users/new' +# # end +# # end -get '/users/:id' do - @user = User.find_by(id: params[:id]) - # @decks = Deck.find_by(user_id: @user.id) - if current_user == @user - erb :'/users/show' - else - redirect '/' - end -end +# get '/users/:id' do +# @user = User.find_by(id: params[:id]) +# # @decks = Deck.find_by(user_id: @user.id) +# if current_user == @user +# erb :'/users/show' +# else +# redirect '/' +# end +# end From e00a8a698f6463cf642983e45bdeed1f7d48a76c Mon Sep 17 00:00:00 2001 From: Riley Scheid Date: Fri, 13 May 2016 17:10:51 -0700 Subject: [PATCH 8/8] Fix form login --- app/controllers/index_controller.rb | 1 - app/views/_login.erb | 2 +- app/views/_register.erb | 2 +- public/js/application.js | 3 ++- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/index_controller.rb b/app/controllers/index_controller.rb index 0caf83e..28d795a 100644 --- a/app/controllers/index_controller.rb +++ b/app/controllers/index_controller.rb @@ -24,7 +24,6 @@ end post '/users' do - p params['user'] @user = User.new(params['user']) p @user if @user.save diff --git a/app/views/_login.erb b/app/views/_login.erb index f10e662..ba385bd 100644 --- a/app/views/_login.erb +++ b/app/views/_login.erb @@ -16,7 +16,7 @@