Skip to content

Commit 8dbc23f

Browse files
Redirect to @record or path in controller generator (#569)
1 parent 2ca1f4c commit 8dbc23f

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

lib/generators/rails/templates/controller.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def create
3030

3131
respond_to do |format|
3232
if @<%= orm_instance.save %>
33-
format.html { redirect_to <%= show_helper %>, notice: <%= %("#{human_name} was successfully created.") %> }
33+
format.html { redirect_to <%= redirect_resource_name %>, notice: <%= %("#{human_name} was successfully created.") %> }
3434
format.json { render :show, status: :created, location: <%= "@#{singular_table_name}" %> }
3535
else
3636
format.html { render :new, status: :unprocessable_entity }
@@ -43,7 +43,7 @@ def create
4343
def update
4444
respond_to do |format|
4545
if @<%= orm_instance.update("#{singular_table_name}_params") %>
46-
format.html { redirect_to <%= show_helper %>, notice: <%= %("#{human_name} was successfully updated.") %> }
46+
format.html { redirect_to <%= redirect_resource_name %>, notice: <%= %("#{human_name} was successfully updated.") %> }
4747
format.json { render :show, status: :ok, location: <%= "@#{singular_table_name}" %> }
4848
else
4949
format.html { render :edit, status: :unprocessable_entity }
@@ -57,7 +57,7 @@ def destroy
5757
@<%= orm_instance.destroy %>
5858
5959
respond_to do |format|
60-
format.html { redirect_to <%= index_helper %>_url, notice: <%= %("#{human_name} was successfully destroyed.") %> }
60+
format.html { redirect_to <%= index_helper %>_path, notice: <%= %("#{human_name} was successfully destroyed.") %> }
6161
format.json { head :no_content }
6262
end
6363
end

test/scaffold_controller_generator_test.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,22 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
3131
assert_instance_method :create, content do |m|
3232
assert_match %r{@post = Post\.new\(post_params\)}, m
3333
assert_match %r{@post\.save}, m
34-
assert_match %r{format\.html \{ redirect_to post_url\(@post\), notice: "Post was successfully created\." \}}, m
34+
assert_match %r{format\.html \{ redirect_to @post, notice: "Post was successfully created\." \}}, m
3535
assert_match %r{format\.json \{ render :show, status: :created, location: @post \}}, m
3636
assert_match %r{format\.html \{ render :new, status: :unprocessable_entity \}}, m
3737
assert_match %r{format\.json \{ render json: @post\.errors, status: :unprocessable_entity \}}, m
3838
end
3939

4040
assert_instance_method :update, content do |m|
41-
assert_match %r{format\.html \{ redirect_to post_url\(@post\), notice: "Post was successfully updated\." \}}, m
41+
assert_match %r{format\.html \{ redirect_to @post, notice: "Post was successfully updated\." \}}, m
4242
assert_match %r{format\.json \{ render :show, status: :ok, location: @post \}}, m
4343
assert_match %r{format\.html \{ render :edit, status: :unprocessable_entity \}}, m
4444
assert_match %r{format\.json \{ render json: @post.errors, status: :unprocessable_entity \}}, m
4545
end
4646

4747
assert_instance_method :destroy, content do |m|
4848
assert_match %r{@post\.destroy}, m
49-
assert_match %r{format\.html \{ redirect_to posts_url, notice: "Post was successfully destroyed\." \}}, m
49+
assert_match %r{format\.html \{ redirect_to posts_path, notice: "Post was successfully destroyed\." \}}, m
5050
assert_match %r{format\.json \{ head :no_content \}}, m
5151
end
5252

@@ -64,15 +64,15 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
6464
run_generator %w(Admin::Post --model-name=Post)
6565
assert_file 'app/controllers/admin/posts_controller.rb' do |content|
6666
assert_instance_method :create, content do |m|
67-
assert_match %r{format\.html \{ redirect_to admin_post_url\(@post\), notice: "Post was successfully created\." \}}, m
67+
assert_match %r{format\.html \{ redirect_to \[:admin, @post\], notice: "Post was successfully created\." \}}, m
6868
end
6969

7070
assert_instance_method :update, content do |m|
71-
assert_match %r{format\.html \{ redirect_to admin_post_url\(@post\), notice: "Post was successfully updated\." \}}, m
71+
assert_match %r{format\.html \{ redirect_to \[:admin, @post\], notice: "Post was successfully updated\." \}}, m
7272
end
7373

7474
assert_instance_method :destroy, content do |m|
75-
assert_match %r{format\.html \{ redirect_to admin_posts_url, notice: "Post was successfully destroyed\." \}}, m
75+
assert_match %r{format\.html \{ redirect_to admin_posts_path, notice: "Post was successfully destroyed\." \}}, m
7676
end
7777
end
7878
end

0 commit comments

Comments
 (0)