Skip to content

Instantly share code, notes, and snippets.

@Evanto
Last active July 11, 2017 16:45
Show Gist options
  • Save Evanto/bba744bdd8bcd453f37e45e70d0090ca to your computer and use it in GitHub Desktop.
Save Evanto/bba744bdd8bcd453f37e45e70d0090ca to your computer and use it in GitHub Desktop.
Ajax answer update works
- if answer.persisted?
div class="answer-#{answer.id}"
= answer.body
- if current_user&.author_of? answer
p= link_to 'Edit', '', class: 'edit-answer-link', data: { answer_id: answer.id }
p= link_to "delete answer", answer_path(answer), method: :delete
p
= form_for [answer], remote: true, html: { id: "edit-answer-#{answer.id}"} do |f|
= f.label :body, "Answer"
= f.text_area :body
= f.submit 'Save'
- @question.answers.each do |answer|
div class="answer-#{answer.id}"
- if answer.persisted?
p= answer.body
- if current_user&.author_of? answer
p= link_to 'Edit', '', class: 'edit-answer-link', data: { answer_id: answer.id }
p= link_to "delete answer", answer_path(answer), method: :delete
p
= form_for [answer], remote: true, html: { id: "edit-answer-#{answer.id}"} do |f|
= f.label :body, "Answer"
= f.text_area :body
= f.submit 'Save'
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
# Объявляем функцию ready, внутри которой можно поместить обработчики событий и другой код,
# который должен выполняться при загрузке страницы
ready = ->
$('.answers').on 'click', '.edit-answer-link', (e) ->
e.preventDefault();
$(this).hide();
answer_id = $(this).data('answerId'); # answerId берется из дива
$('form#edit-form').hide();
$('form#edit-answer-' + answer_id).show();
$(document).ready(ready); # вешаем функцию ready на событие document.ready
$(document).on('turbolinks:load', ready); # вешаем функцию ready на событие page:load
$(document).on('turbolinks:update', ready); # вешаем функцию ready на событие page:update
class AnswersController < ApplicationController
before_action :authenticate_user!
before_action :load_question, only: [:create]
before_action :load_answer, only: [:edit, :update, :destroy]
def edit
end
def update
if current_user&.author_of? @answer
#if current_user.id == @answer.user_id
@answer.update(answer_params)
@question = @answer.question
end
end
def create
@answer = @question.answers.new(answer_params)
@answer.user = current_user
@answer.save
end
def destroy
@question = @answer.question
if current_user.author_of? @answer
@answer.destroy!
flash[:notice] = 'Your answer was successfully deleted.'
redirect_to question_path(@question)
else
redirect_to question_path(@question)
end
end
private
def load_question
@question = Question.find(params[:question_id])
end
def load_answer
@answer = Answer.find(params[:id])
end
def answer_params
params.require(:answer).permit(:body)
end
end
$('.answers').append('<%= j render @answer %>');
$('form#new_answer #answer_body').val('');
$('.errors').html("<%= j render 'shared/errors', object: @answer %>");
h1 = @question.title
p = @question.body
- if current_user&.author_of? @question
= link_to "delete question", question_path(@question), method: :delete
h2 Answers
.answers
= render 'answers/answers'
- if user_signed_in?
.errors
= render 'answers/form'
<% if @answer.errors.present? %>
<% @answer.errors.full_messages.each do |message| %>
$('.errors').html('<%= j message %>');
<% end %>
<% else %>
$('#answer<%= @answer.id %>').replaceWith('<%= j render @answer %>');
$('.answers').html("<%= j render 'answers' %>");
$('.edit-answer-link').click(function(e) {
e.preventDefault();
$(this).hide();
answer_id = $(this).data('answerId');
$('form#edit-answer-' + answer_id).show();
});
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment