Skip to content

Instantly share code, notes, and snippets.

@Evanto
Last active July 10, 2017 10:59
Show Gist options
  • Save Evanto/7b147733f585ec276d741d279fa0f007 to your computer and use it in GitHub Desktop.
Save Evanto/7b147733f585ec276d741d279fa0f007 to your computer and use it in GitHub Desktop.
форма не появляется по клику на edit свежедобавленного ответа
# 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 = -> # оборачиваем все до строки 15 в функцию ready
# Это наш функция-обработчик, перенесенная сюда из document.ready ($ ->)
$('.edit-answer-link').click (e) -> # событие ниже работает по клику на элемент с классом .edit-answer-link
# (почитать, что делает событие click в jquery) https://api.jquery.com/click/
e.preventDefault(); # что это означает? прочитать
$(this).hide(); # хайдит ссылку .edit-answer-link.
answer_id = $(this).data('answerId'); # получает данные (id) из data и сохраняет в переменную answer_id
# $('form#edit-form').hide(); # пометить форму редактирования ответа айлишником и вписать сюда.
# тут хайдим все формы кроме той что соответствует айди ответа у которого кликнули edit
$('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
# хендлером называется функция, повешенная на какое-то событие (исполняемая по событию)
# 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 = ->
# Это наш обработчик, перенесенный сюда из document.ready ($ ->)
$('.answers').on 'click', '.edit-answer-link', (e) ->
e.preventDefault();
$(this).hide();
answer_id = $(this).data('answerId')
$('form#edit-answer-' + answer_id).show()
$(document).on('turbolinks:load', ready)
$(document).on('page:load', ready)
$(document).on('page:update', ready)
<% 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/answers' %>');
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment