-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from sad16/7-ajax
ajax
- Loading branch information
Showing
45 changed files
with
548 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
$(document).on('turbolinks:load', function(){ | ||
$('.answers').on('click', '.edit-answer-link', function(e) { | ||
e.preventDefault(); | ||
$(this).addClass('hidden'); | ||
var answerId = $(this).data('answerId'); | ||
$('form#edit-answer-form-' + answerId).removeClass('hidden'); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,5 @@ | |
//= require rails-ujs | ||
//= require activestorage | ||
//= require turbolinks | ||
//= require jquery3 | ||
//= require_tree . |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
$(document).on('turbolinks:load', function(){ | ||
$('.question').on('click', '.edit-question-link', function(e) { | ||
e.preventDefault(); | ||
$(this).addClass('hidden'); | ||
var questionId = $(this).data('questionId'); | ||
$('form#edit-question-form-' + questionId).removeClass('hidden'); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
// Place all the styles related to the Answers controller here. | ||
// They will automatically be included in application.css. | ||
// You can use Sass (SCSS) here: http://sass-lang.com/ | ||
|
||
.answers { | ||
.best { | ||
border: 2px solid green; | ||
|
||
.mark-as-best { | ||
display: none; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.hidden { | ||
display: none; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
class Question < ApplicationRecord | ||
belongs_to :user | ||
has_one :best_answer, class_name: 'Answer', dependent: :nullify | ||
has_many :answers, dependent: :destroy | ||
|
||
validates :title, :body, presence: true | ||
|
||
def answers_without_best | ||
answers.where.not(id: best_answer_id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
- if answer.persisted? | ||
.answer id="answer-id-#{answer.id}" class=("best" if local_assigns[:best]) | ||
p = answer.body | ||
|
||
- if user_signed_in? | ||
- if current_user.author_of?(answer.question) | ||
p.mark-as-best = link_to "Mark as best", mark_as_best_answer_path(answer), method: :post, remote: true | ||
|
||
- if current_user.author_of?(answer) | ||
p = link_to "Edit answer", "#", class: "edit-answer-link", data: { answer_id: answer.id } | ||
|
||
.edit-answer-form | ||
.answer-errors | ||
= render "answers/edit_form", answer: answer | ||
|
||
p = link_to "Delete answer", answer_path(answer), method: :delete, remote: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
= form_with model: answer, class: "hidden", html: { id: "edit-answer-form-#{answer.id}" } do |f| | ||
p | ||
= f.label :body | ||
= f.text_field :body | ||
p | ||
= f.submit "Save" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
= form_with model: [ @question, @answer ], local: true do |f| | ||
= form_with model: [@question, @answer] do |f| | ||
p | ||
= f.label :body | ||
= f.text_field :body | ||
p | ||
= f.submit 'Answer' | ||
= f.submit "Answer" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
$(".new-answer-form .answer-errors").html("<%= render "shared/errors", resource: @answer %>"); | ||
|
||
<% if @answer.persisted? %> | ||
$(".answers").append("<%= j render @answer %>"); | ||
$(".new-answer-form #answer_body").val(""); | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<% if @answer.destroyed? %> | ||
$(".answers #answer-id-<%= @answer.id %>").remove(); | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
$(".answers .best .mark-as-best").removeClass("hidden"); | ||
$(".answers .best").removeClass("best"); | ||
$(".answers #answer-id-<%= @answer.id %>").addClass("best"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
$("#answer-id-<%= @answer.id %> .edit-answer-form .answer-errors").html("<%= render 'shared/errors', resource: @answer %>"); | ||
|
||
<% if @answer.errors.empty? %> | ||
$("#answer-id-<%= @answer.id %>").replaceWith("<%= j render @answer, best: @answer.best? %>"); | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
$('.notice').html('<%= @notice %>'); | ||
$('.alert').html('<%= @alert %>'); | ||
|
||
<%= yield %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
= form_with model: @question, class: "hidden", html: {id: "edit-question-form-#{@question.id}"} do |f| | ||
p | ||
= f.label :title | ||
= f.text_field :title | ||
p | ||
= f.label :body | ||
= f.text_area :body | ||
p | ||
= f.submit "Save" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
= form_with model: @question, local: true do |f| | ||
p | ||
= f.label :title | ||
= f.text_field :title | ||
p | ||
= f.label :body | ||
= f.text_area :body | ||
p | ||
= f.submit "Ask" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,3 @@ | ||
= render '/shared/errors', resource: @question | ||
= render "/shared/errors", resource: @question | ||
|
||
= form_with model: @question, local: true do |f| | ||
p | ||
= f.label :title | ||
= f.text_field :title | ||
p | ||
= f.label :body | ||
= f.text_area :body | ||
p | ||
= f.submit 'Ask' | ||
= render "/questions/form" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,26 @@ | ||
p = link_to "Questions", questions_path | ||
|
||
h1 = @question.title | ||
p = @question.body | ||
.question | ||
h1.title = @question.title | ||
p.body = @question.body | ||
|
||
- if user_signed_in? | ||
- if current_user.author_of?(@question) | ||
p = link_to "Delete question", question_path(@question), method: :delete | ||
- if user_signed_in? | ||
- if current_user.author_of?(@question) | ||
p = link_to "Edit question", "#", class: "edit-question-link", data: {question_id: @question.id} | ||
|
||
p New answer | ||
= render '/shared/errors', resource: @answer | ||
= render '/answers/form' | ||
.edit-question-form | ||
.question-errors | ||
= render "/questions/edit_form" | ||
|
||
p Answers | ||
- if @question.answers.present? | ||
- @question.answers.each do |answer| | ||
- if answer.persisted? | ||
p = answer.body | ||
p = link_to "Delete question", question_path(@question), method: :delete | ||
|
||
p New answer | ||
.new-answer-form | ||
.answer-errors | ||
= render "/answers/form" | ||
|
||
- if current_user&.author_of?(answer) | ||
p = link_to "Delete answer", answer_path(answer), method: :delete | ||
p Answers | ||
.answers | ||
- if @question.best_answer_id | ||
= render @question.best_answer, best: true | ||
= render @question.answers_without_best |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
$(".edit-question-form .question-errors").html("<%= render "shared/errors", resource: @question %>"); | ||
|
||
<% if @question.errors.empty? %> | ||
$(".question .title").html("<%= j @question.title %>"); | ||
$(".question .body").html("<%= j @question.body %>"); | ||
$(".question .edit-question-link").removeClass("hidden"); | ||
$("form#edit-question-form-<%= @question.id %>").addClass("hidden"); | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddBestAnswerIdToQuestions < ActiveRecord::Migration[5.2] | ||
def change | ||
add_column :questions, :best_answer_id, :integer | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.