-
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 #5 from metacorn/Part_6_AJAX_in_Rails
Part 6. AJAX in Rails.
- Loading branch information
Showing
44 changed files
with
744 additions
and
150 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-links', function(e) { | ||
e.preventDefault() | ||
$(this).hide() | ||
var answerId = $(this).data('answerId') | ||
$('form#edit-answer-' + answerId).show() | ||
}) | ||
}) |
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() { | ||
$('.edit-question-link').on('click', function(e) { | ||
e.preventDefault() | ||
$(this).hide() | ||
var questionId = $(this).data('questionId') | ||
$('form#edit-question-' + questionId).show() | ||
}) | ||
}) |
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
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
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,2 +1,12 @@ | ||
p= answer.body | ||
p= render(partial: 'shared/manage_answer_links', locals: { answer: answer }) if current_user&.owned?(answer) | ||
div id="answer_#{answer.id}" | ||
- if answer.persisted? | ||
- if answer.best? | ||
div id="best-answer" | ||
h4 The best answer | ||
p= answer.body | ||
p= render(partial: 'shared/answer_links', locals: { answer: answer }) | ||
|
||
= form_with model: answer, class: 'hidden', html: { id: "edit-answer-#{answer.id}" } do |f| | ||
= f.label :body, 'Your answer' | ||
= f.text_area :body | ||
= f.submit 'Update' |
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-errors').html('<%= render 'shared/errors', resource: @answer %>') | ||
<% if @answer.persisted? %> | ||
$('.answers').append('<%= j render @answer %>') | ||
$('.new-answer #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 @@ | ||
$('#answer_' + <%= @answer.id %>).remove() |
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 @@ | ||
$('.answers').html('<%= j render @question.answers %>') |
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 @@ | ||
$('.answer-errors').html('<%= render 'shared/errors', resource: @answer %>') | ||
<% if @answer.errors.empty? %> | ||
$("#answer_" + <%= @answer.id %>).replaceWith('<%= j render @answer %>') | ||
<% 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,11 @@ | ||
h4= @question.title | ||
p= @question.body | ||
=render(partial: 'shared/question_links', locals: { question: @question }) if current_user&.owner?(@question) | ||
.question-errors | ||
=render 'shared/errors', resource: @question | ||
= form_with model: @question, class: 'hidden', html: { id: "edit-question-#{@question.id}" } do |f| | ||
= f.label :title | ||
= f.text_field :title | ||
= f.label :body, 'Body' | ||
= f.text_area :body | ||
= f.submit 'Update' |
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,12 +1,12 @@ | ||
= render 'shared/errors', resource: @answer | ||
|
||
=render(partial: 'shared/manage_question_links', locals: { question: @question }) if current_user&.owned?(@question) | ||
h4= @question.title | ||
p= @question.body | ||
p Answers | ||
p= render @question.reload.answers | ||
div id="question_#{@question.id}" | ||
= render 'question_for_show', resource: @question | ||
h4 Answers | ||
div.answers | ||
= render @question.answers | ||
p Your answer | ||
= form_with model: @answer, url: question_answers_path(@question), local: true do |f| | ||
.answer-errors | ||
= render 'shared/errors', resource: @answer | ||
= form_with model: [@question, @answer], class: 'new-answer' do |f| | ||
= f.label :body | ||
= f.text_area :body | ||
= f.submit 'Leave' |
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 @@ | ||
$('.question-errors').html('<%= render 'shared/errors', resource: @question %>') | ||
<% if !@question.errors.present? %> | ||
$("#question_" + <%= @question.id %>).html('<%= j render 'question_for_show', resource: @question %>') | ||
<% 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,7 @@ | ||
p | ||
=> link_to('Mark as the best', mark_answer_path(answer), method: :post, remote: true) \ | ||
if current_user&.owner?(answer.question) && !answer.best? | ||
=> link_to('Edit', '#', class: 'edit-answer-links', data: { answer_id: answer.id }) \ | ||
if current_user&.owner?(answer) | ||
= link_to('Delete', answer_path(answer), method: :delete, remote: true) \ | ||
if current_user&.owner?(answer) |
This file was deleted.
Oops, something went wrong.
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,3 @@ | ||
p | ||
=> link_to 'Edit', '#', class: 'edit-question-link', data: { question_id: question.id } | ||
= link_to 'Delete', question_path(question), method: :delete |
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 +1,7 @@ | ||
p= link_to 'Log out', destroy_user_session_path, method: :delete | ||
- if current_user | ||
p= link_to 'Log out', destroy_user_session_path, method: :delete | ||
- else | ||
p | ||
= link_to 'Log in', new_user_session_path | ||
| | | ||
= link_to 'Sign up', new_user_registration_path |
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 AddBestToAnswers < ActiveRecord::Migration[5.2] | ||
def change | ||
add_column :answers, :best, :boolean, default: false | ||
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.