Skip to content

Commit 496a2f9

Browse files
ceritiumdhh
andauthored
Fix scaffold controller generator with namespace (#512)
* Fix scaffold controller generator with namespace For example: ``` bundle exec rails g scaffold_controller Admin::Post title:string content:text --model-name=Post ``` It fixes the code generated by jbuilder, other changes are required on railties gem. * Specify main branch for rails-head * Add test for controller scaffold with namespace Only for Rails::VERSION::MAJOR >= 6 because the method `show_helper` doesn't handle well the namespaces on previous versions. Co-authored-by: David Heinemeier Hansson <[email protected]>
1 parent a8a1741 commit 496a2f9

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

lib/generators/rails/templates/controller.rb

Lines changed: 2 additions & 2 deletions
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 @<%= singular_table_name %>, notice: <%= %("#{human_name} was successfully created.") %> }
33+
format.html { redirect_to <%= show_helper %>, 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 @<%= singular_table_name %>, notice: <%= %("#{human_name} was successfully updated.") %> }
46+
format.html { redirect_to <%= show_helper %>, 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 }

test/scaffold_controller_generator_test.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ 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, notice: "Post was successfully created\." \}}, m
34+
assert_match %r{format\.html \{ redirect_to post_url\(@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, notice: "Post was successfully updated\." \}}, m
41+
assert_match %r{format\.html \{ redirect_to post_url\(@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
@@ -59,6 +59,25 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
5959
end
6060
end
6161

62+
if Rails::VERSION::MAJOR >= 6
63+
test 'controller with namespace' do
64+
run_generator %w(Admin::Post --model-name=Post)
65+
assert_file 'app/controllers/admin/posts_controller.rb' do |content|
66+
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
68+
end
69+
70+
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
72+
end
73+
74+
assert_instance_method :destroy, content do |m|
75+
assert_match %r{format\.html \{ redirect_to admin_posts_url, notice: "Post was successfully destroyed\." \}}, m
76+
end
77+
end
78+
end
79+
end
80+
6281
test "don't use require and permit if there are no attributes" do
6382
run_generator %w(Post)
6483

0 commit comments

Comments
 (0)