From b0bf7695b621b825cc62e885d61ae28b5f8ca7a8 Mon Sep 17 00:00:00 2001 From: Taketo Takashima Date: Tue, 2 Sep 2025 16:29:20 +0900 Subject: [PATCH] Use :unprocessable_content for scaffolds with Rack 3.1 or higher --- .../rails/scaffold_controller_generator.rb | 4 ++++ .../rails/templates/api_controller.rb | 4 ++-- lib/generators/rails/templates/controller.rb | 8 ++++---- test/scaffold_api_controller_generator_test.rb | 12 ++++++++++-- test/scaffold_controller_generator_test.rb | 18 ++++++++++++++---- 5 files changed, 34 insertions(+), 12 deletions(-) diff --git a/lib/generators/rails/scaffold_controller_generator.rb b/lib/generators/rails/scaffold_controller_generator.rb index 42407c09..36658998 100644 --- a/lib/generators/rails/scaffold_controller_generator.rb +++ b/lib/generators/rails/scaffold_controller_generator.rb @@ -15,6 +15,10 @@ class ScaffoldControllerGenerator def permitted_params attributes_names.map { |name| ":#{name}" }.join(', ') end unless private_method_defined? :permitted_params + + def status_unprocessable_content + ::Rack::Utils::SYMBOL_TO_STATUS_CODE.key(422) rescue :unprocessable_content + end end end end diff --git a/lib/generators/rails/templates/api_controller.rb b/lib/generators/rails/templates/api_controller.rb index 5c2222cc..62cb8a65 100644 --- a/lib/generators/rails/templates/api_controller.rb +++ b/lib/generators/rails/templates/api_controller.rb @@ -25,7 +25,7 @@ def create if @<%= orm_instance.save %> render :show, status: :created, location: <%= "@#{singular_table_name}" %> else - render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity + render json: <%= "@#{orm_instance.errors}" %>, status: :<%= status_unprocessable_content.to_s %> end end @@ -35,7 +35,7 @@ def update if @<%= orm_instance.update("#{singular_table_name}_params") %> render :show, status: :ok, location: <%= "@#{singular_table_name}" %> else - render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity + render json: <%= "@#{orm_instance.errors}" %>, status: :<%= status_unprocessable_content.to_s %> end end diff --git a/lib/generators/rails/templates/controller.rb b/lib/generators/rails/templates/controller.rb index 0aff8215..b0b56d92 100644 --- a/lib/generators/rails/templates/controller.rb +++ b/lib/generators/rails/templates/controller.rb @@ -33,8 +33,8 @@ def create format.html { redirect_to <%= redirect_resource_name %>, notice: <%= %("#{human_name} was successfully created.") %> } format.json { render :show, status: :created, location: <%= "@#{singular_table_name}" %> } else - format.html { render :new, status: :unprocessable_entity } - format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity } + format.html { render :new, status: :<%= status_unprocessable_content.to_s %> } + format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :<%= status_unprocessable_content.to_s %> } end end end @@ -46,8 +46,8 @@ def update format.html { redirect_to <%= redirect_resource_name %>, notice: <%= %("#{human_name} was successfully updated.") %>, status: :see_other } format.json { render :show, status: :ok, location: <%= "@#{singular_table_name}" %> } else - format.html { render :edit, status: :unprocessable_entity } - format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity } + format.html { render :edit, status: :<%= status_unprocessable_content.to_s %> } + format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :<%= status_unprocessable_content.to_s %> } end end end diff --git a/test/scaffold_api_controller_generator_test.rb b/test/scaffold_api_controller_generator_test.rb index 546749b1..932ad1b3 100644 --- a/test/scaffold_api_controller_generator_test.rb +++ b/test/scaffold_api_controller_generator_test.rb @@ -24,12 +24,20 @@ class ScaffoldApiControllerGeneratorTest < Rails::Generators::TestCase assert_match %r{@post = Post\.new\(post_params\)}, m assert_match %r{@post\.save}, m assert_match %r{render :show, status: :created, location: @post}, m - assert_match %r{render json: @post\.errors, status: :unprocessable_entity}, m + if Gem::Version.new(Rack::RELEASE) < Gem::Version.new("3.1") + assert_match %r{render json: @post\.errors, status: :unprocessable_entity}, m + else + assert_match %r{render json: @post\.errors, status: :unprocessable_content}, m + end end assert_instance_method :update, content do |m| assert_match %r{render :show, status: :ok, location: @post}, m - assert_match %r{render json: @post.errors, status: :unprocessable_entity}, m + if Gem::Version.new(Rack::RELEASE) < Gem::Version.new("3.1") + assert_match %r{render json: @post.errors, status: :unprocessable_entity}, m + else + assert_match %r{render json: @post.errors, status: :unprocessable_content}, m + end end assert_instance_method :destroy, content do |m| diff --git a/test/scaffold_controller_generator_test.rb b/test/scaffold_controller_generator_test.rb index 048df2a3..e68aa252 100644 --- a/test/scaffold_controller_generator_test.rb +++ b/test/scaffold_controller_generator_test.rb @@ -33,15 +33,25 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase assert_match %r{@post\.save}, m assert_match %r{format\.html \{ redirect_to @post, notice: "Post was successfully created\." \}}, m assert_match %r{format\.json \{ render :show, status: :created, location: @post \}}, m - assert_match %r{format\.html \{ render :new, status: :unprocessable_entity \}}, m - assert_match %r{format\.json \{ render json: @post\.errors, status: :unprocessable_entity \}}, m + if Gem::Version.new(Rack::RELEASE) < Gem::Version.new("3.1") + assert_match %r{format\.html \{ render :new, status: :unprocessable_entity \}}, m + assert_match %r{format\.json \{ render json: @post\.errors, status: :unprocessable_entity \}}, m + else + assert_match %r{format\.html \{ render :new, status: :unprocessable_content \}}, m + assert_match %r{format\.json \{ render json: @post\.errors, status: :unprocessable_content \}}, m + end end assert_instance_method :update, content do |m| assert_match %r{format\.html \{ redirect_to @post, notice: "Post was successfully updated\.", status: :see_other \}}, m assert_match %r{format\.json \{ render :show, status: :ok, location: @post \}}, m - assert_match %r{format\.html \{ render :edit, status: :unprocessable_entity \}}, m - assert_match %r{format\.json \{ render json: @post.errors, status: :unprocessable_entity \}}, m + if Gem::Version.new(Rack::RELEASE) < Gem::Version.new("3.1") + assert_match %r{format\.html \{ render :edit, status: :unprocessable_entity \}}, m + assert_match %r{format\.json \{ render json: @post.errors, status: :unprocessable_entity \}}, m + else + assert_match %r{format\.html \{ render :edit, status: :unprocessable_content \}}, m + assert_match %r{format\.json \{ render json: @post.errors, status: :unprocessable_content \}}, m + end end assert_instance_method :destroy, content do |m|