Skip to content

Instantly share code, notes, and snippets.

@Evanto
Created July 5, 2017 11:20
Show Gist options
  • Save Evanto/70a16fb84741159bdc1572627b2f7124 to your computer and use it in GitHub Desktop.
Save Evanto/70a16fb84741159bdc1572627b2f7124 to your computer and use it in GitHub Desktop.
404 form doesn't save an updated answer
- 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 [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/
$ ->
$('.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, :update]
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
class QuestionsController < ApplicationController
before_action :authenticate_user!, only: [:new, :create, :edit, :update, :destroy]
before_action :load_question, only: [:show, :edit, :update, :destroy]
def index
@questions = Question.all
end
def show
end
def new
@question = Question.new
end
def edit
end
def create
@question = current_user.questions.new(question_params)
if @question.save
flash[:notice] = 'Your question was successfully created.'
redirect_to @question
else
render :new
end
end
def update
if @question.update(question_params)
redirect_to @question
else
render :edit
end
end
def destroy
if current_user.author_of? @question
@question.destroy
flash[:notice] = 'Your question was successfully deleted.'
redirect_to questions_path
else
redirect_to questions_path
end
end
private
def load_question
@question = Question.find(params[:id])
end
def question_params
params.require(:question).permit(:title, :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 'answers/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