[deploy] Remove redirect on comments#update (#10337)

* Remove redirect on comments#update

* Delete unecessary view

* Refactor to account for orgs and podcasts

* Code cleanup

* Add specs

* Fix podcast comment update

* Add more safe navigation operators

* Update safe nav operators again

* Update comment to explain more
This commit is contained in:
Alex 2020-09-29 15:33:51 -04:00 committed by GitHub
parent 877e9a13b1
commit 1fbd20087a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 1 deletions

View file

@ -141,7 +141,33 @@ class CommentsController < ApplicationController
authorize @comment
if @comment.update(permitted_attributes(@comment).merge(edited_at: Time.zone.now))
Mention.create_all(@comment)
redirect_to URI.parse(@comment.path).path, notice: "Comment was successfully updated."
# The following sets variables used in the index view. We render the
# index view directly to avoid having to redirect.
#
# Redirects lead to a race condition where we redirect to a cached view
# after updating data and we don't bust the cache fast enough before
# hitting the view, therefore stale content ends up being served from
# cache.
#
# https://github.com/forem/forem/issues/10338#issuecomment-693401481
@on_comments_page = true
@root_comment = @comment
@commentable = @comment.commentable
@commentable_type = @comment.commentable_type
case @commentable_type
when "PodcastEpisode"
@user = @commentable&.podcast
when "Article"
# user could be a user or an organization
@user = @commentable&.user
@article = @commentable
else
@user = @commentable&.user
end
render :index
else
@commentable = @comment.commentable
render :edit

View file

@ -17,4 +17,13 @@ RSpec.describe "CommentsUpdate", type: :request do
}
expect(Comment.last.processed_html).to include(new_body)
end
it "doesn't redirect" do
new_body = "NEW TITLE #{rand(100)}"
put "/comments/#{comment.id}", params: {
comment: { body_markdown: new_body }
}
expect(response).not_to have_http_status(:redirect)
expect(response).to have_http_status(:ok)
end
end