diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 0949211e2..28bb9d3b4 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -156,11 +156,11 @@ class ArticlesController < ApplicationController @user = current_user article = Articles::Creator.call(@user, article_params_json) - render json: if article.persisted? - { id: article.id, current_state_path: article.decorate.current_state_path }.to_json - else - article.errors.to_json - end + if article.persisted? + render json: { id: article.id, current_state_path: article.decorate.current_state_path }, status: :ok + else + render json: article.errors.to_json, status: :unprocessable_entity + end end def update @@ -188,11 +188,11 @@ class ArticlesController < ApplicationController end format.json do - render json: if updated.success - @article.to_json(only: [:id], methods: [:current_state_path]) - else - @article.errors.to_json - end + if updated.success + render json: @article.to_json(only: [:id], methods: [:current_state_path]), status: :ok + else + render json: @article.errors.to_json, status: :unprocessable_entity + end end end end diff --git a/spec/requests/articles/articles_create_spec.rb b/spec/requests/articles/articles_create_spec.rb index 6ec4827c9..e040e444f 100644 --- a/spec/requests/articles/articles_create_spec.rb +++ b/spec/requests/articles/articles_create_spec.rb @@ -196,4 +196,11 @@ RSpec.describe "ArticlesCreate" do expect(a.published_at).to be_within(1.minute).of(published_at) end end + + context "when validation error" do + it "returns 422 status code" do + post "/articles", params: { article: { body_markdown: nil } } + expect(response).to have_http_status(:unprocessable_entity) + end + end end diff --git a/spec/requests/articles/articles_update_spec.rb b/spec/requests/articles/articles_update_spec.rb index 4e0919850..3e2428a82 100644 --- a/spec/requests/articles/articles_update_spec.rb +++ b/spec/requests/articles/articles_update_spec.rb @@ -26,6 +26,15 @@ RSpec.describe "ArticlesUpdate" do expect(article.reload.title).to eq(new_title) end + it "returns an unprocessable status with invalid params" do + put "/articles/#{article.id}", params: { + article: { title: "", body_markdown: "Hello World" }, + format: :json + } + + expect(response).to have_http_status(:unprocessable_entity) + end + it "updates article with front matter params" do put "/articles/#{article.id}", params: { article: {