From b684c8152636c899a990df383a5db88c74714b63 Mon Sep 17 00:00:00 2001 From: Jerome Dalbert Date: Sun, 1 Sep 2024 04:11:57 -0700 Subject: [PATCH] Change controller generators to use _path instead of _url for redirects --- lib/generators/rails/templates/controller.rb | 6 +++--- test/scaffold_controller_generator_test.rb | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/generators/rails/templates/controller.rb b/lib/generators/rails/templates/controller.rb index 92f5da78..0dca2c98 100644 --- a/lib/generators/rails/templates/controller.rb +++ b/lib/generators/rails/templates/controller.rb @@ -30,7 +30,7 @@ def create respond_to do |format| if @<%= orm_instance.save %> - format.html { redirect_to <%= show_helper %>, notice: <%= %("#{human_name} was successfully created.") %> } + format.html { redirect_to <%= "#{singular_route_name}_path(@#{singular_table_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 } @@ -43,7 +43,7 @@ def create def update respond_to do |format| if @<%= orm_instance.update("#{singular_table_name}_params") %> - format.html { redirect_to <%= show_helper %>, notice: <%= %("#{human_name} was successfully updated.") %> } + format.html { redirect_to <%= "#{singular_route_name}_path(@#{singular_table_name})" %>, notice: <%= %("#{human_name} was successfully updated.") %> } format.json { render :show, status: :ok, location: <%= "@#{singular_table_name}" %> } else format.html { render :edit, status: :unprocessable_entity } @@ -57,7 +57,7 @@ def destroy @<%= orm_instance.destroy %> respond_to do |format| - format.html { redirect_to <%= index_helper %>_url, notice: <%= %("#{human_name} was successfully destroyed.") %> } + format.html { redirect_to <%= "#{plural_route_name}_path" %>, notice: <%= %("#{human_name} was successfully destroyed.") %> } format.json { head :no_content } end end diff --git a/test/scaffold_controller_generator_test.rb b/test/scaffold_controller_generator_test.rb index 10bf26bf..545649d7 100644 --- a/test/scaffold_controller_generator_test.rb +++ b/test/scaffold_controller_generator_test.rb @@ -31,14 +31,14 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase assert_instance_method :create, content do |m| assert_match %r{@post = Post\.new\(post_params\)}, m assert_match %r{@post\.save}, m - assert_match %r{format\.html \{ redirect_to post_url\(@post\), notice: "Post was successfully created\." \}}, m + assert_match %r{format\.html \{ redirect_to post_path\(@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 end assert_instance_method :update, content do |m| - assert_match %r{format\.html \{ redirect_to post_url\(@post\), notice: "Post was successfully updated\." \}}, m + assert_match %r{format\.html \{ redirect_to post_path\(@post\), notice: "Post was successfully updated\." \}}, 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 @@ -46,7 +46,7 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase assert_instance_method :destroy, content do |m| assert_match %r{@post\.destroy}, m - assert_match %r{format\.html \{ redirect_to posts_url, notice: "Post was successfully destroyed\." \}}, m + assert_match %r{format\.html \{ redirect_to posts_path, notice: "Post was successfully destroyed\." \}}, m assert_match %r{format\.json \{ head :no_content \}}, m end @@ -64,15 +64,15 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase run_generator %w(Admin::Post --model-name=Post) assert_file 'app/controllers/admin/posts_controller.rb' do |content| assert_instance_method :create, content do |m| - assert_match %r{format\.html \{ redirect_to admin_post_url\(@post\), notice: "Post was successfully created\." \}}, m + assert_match %r{format\.html \{ redirect_to admin_post_path\(@post\), notice: "Post was successfully created\." \}}, m end assert_instance_method :update, content do |m| - assert_match %r{format\.html \{ redirect_to admin_post_url\(@post\), notice: "Post was successfully updated\." \}}, m + assert_match %r{format\.html \{ redirect_to admin_post_path\(@post\), notice: "Post was successfully updated\." \}}, m end assert_instance_method :destroy, content do |m| - assert_match %r{format\.html \{ redirect_to admin_posts_url, notice: "Post was successfully destroyed\." \}}, m + assert_match %r{format\.html \{ redirect_to admin_posts_path, notice: "Post was successfully destroyed\." \}}, m end end end