Skip to content

Instantly share code, notes, and snippets.

@Evanto
Created July 3, 2017 20:00
Show Gist options
  • Save Evanto/bf6f24c9b2c3cef4ba21cc9b2938b5c8 to your computer and use it in GitHub Desktop.
Save Evanto/bf6f24c9b2c3cef4ba21cc9b2938b5c8 to your computer and use it in GitHub Desktop.
Ajax-2-edit-answers
- if answer.persisted?
= answer.body
- if current_user&.author_of? answer
p= link_to "delete answer", answer_path(answer), method: :delete
p= link_to 'edit', '', class: 'edit-answer-link', data: { answer_id: answer.id }
p
- @question.answers.each do |answer|
li id="answer-#{answer.id}"
- if answer.persisted?
= 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 [@question, answer], remote: true, html: { id: "edit-answer-#{answer.id}"} do |f|
= f.label :body, "Answer"
= f.text_area :body
= f.submit 'Save'
= form_for([@question, Answer.new], remote: true) do |f|
p= f.label :body, 'Answer'
p= f.text_area :body
p= f.submit 'Post your answer'
# 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/
$ ->
$('.edit-answer-link').click (e) ->
e.preventDefault();
$(this).hide();
answer_id = $(this).data('answerId');
$('form#edit-answer-' + answer_id).show();
// 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/
.edit_answer { display: none; }
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
@answer = Answer.find(params[:id])
@answer.update(answer_params)
@question = @answer.question
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
h1 = @question.title
p = @question.body
- if current_user&.author_of? @question
= link_to "delete question", question_path(@question), method: :delete
h2 Answers
.answers
ul
= render @question.answers
- if user_signed_in?
.errors
= render 'answers/form'
<% if @answer.errors.present? %>
<% @answer.errors.full_messages.each do |message| %>
$('.answer-errors').html('<%= j message %>');
<% end %>
<% else %>
$('#answer<%= @answer.id %>').replaceWith('<%= j render @answer %>');
$('.answers').html('<%= j render 'answers/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