Fix NoMethodError on Nil for article preview (#16569)

* Fix NoMethodError on Nil for article preview

* Remove commented out debugging code
This commit is contained in:
Jamie Gaskins 2022-02-14 14:38:29 -05:00 committed by GitHub
parent 36acb7c9a0
commit 66846c7a17
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 3 deletions

View file

@ -90,11 +90,19 @@ class ArticlesController < ApplicationController
format.json { render json: @article.errors, status: :unprocessable_entity }
else
format.json do
front_matter = parsed.front_matter.to_h
if front_matter["tags"]
tags = Article.new.tag_list.add(front_matter["tags"], parser: ActsAsTaggableOn::TagParser)
end
if front_matter["cover_image"]
cover_image = ApplicationController.helpers.cloud_cover_url(front_matter["cover_image"])
end
render json: {
processed_html: processed_html,
title: parsed["title"],
tags: (Article.new.tag_list.add(parsed["tags"], parser: ActsAsTaggableOn::TagParser) if parsed["tags"]),
cover_image: (ApplicationController.helpers.cloud_cover_url(parsed["cover_image"]) if parsed["cover_image"])
title: front_matter["title"],
tags: tags,
cover_image: cover_image
}, status: :ok
end
end

View file

@ -66,5 +66,24 @@ RSpec.describe "Editor", type: :request do
expect(response.media_type).to eq("application/json")
end
end
context "with front matter" do
it "returns successfully" do
sign_in user
article_body = <<~MARKDOWN
---
---
Hello
MARKDOWN
post "/articles/preview",
headers: headers,
params: { article_body: article_body },
as: :json
expect(response).to be_successful
end
end
end
end