From 4dcb630c6eaf7e4d8e450b3e9f19e38ebbf41d8b Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Sun, 5 Nov 2017 13:37:21 +0900 Subject: Generate the correct path in nested scaffold generator Currently, namespaced scaffold generator will generate an incorrect path and the generated file will not work properly. ``` $ ./bin/rails g scaffold admin/user $ ./bin/rails db:migrate $ ./bin/rails t test/controllers # Running: E Error: Admin::UsersControllerTest#test_should_create_admin_user: NameError: undefined local variable or method `admin_admin_users_url' for # Did you mean? admin_users test/controllers/admin/users_controller_test.rb:20:in `block (2 levels) in ' test/controllers/admin/users_controller_test.rb:19:in `block in ' bin/rails test test/controllers/admin/users_controller_test.rb:18 ``` This is because combine `controller_class_path` and `singular_table_name` to generate route. https://github.com/rails/rails/blob/360698aa245b45349d1d1b12e1afb34759515e69/railties/lib/rails/generators/named_base.rb#L172 Normally, if using namspaced generator, table name already contains namespace. Therefore, adding `controller_class_path` adds extra namespace. Since it is special only when explicitly specifying `model-name`, it is modified to change the value only when `model-name`is specified. Follow up of #30729 --- railties/lib/rails/generators/named_base.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb index 44f5ab45d3..e0285835a8 100644 --- a/railties/lib/rails/generators/named_base.rb +++ b/railties/lib/rails/generators/named_base.rb @@ -158,26 +158,26 @@ module Rails def model_resource_name(prefix: "") # :doc: resource_name = "#{prefix}#{singular_table_name}" - if controller_class_path.empty? - resource_name - else + if options[:model_name] "[#{controller_class_path.map { |name| ":" + name }.join(", ")}, #{resource_name}]" + else + resource_name end end def singular_route_name # :doc: - if controller_class_path.empty? - singular_table_name - else + if options[:model_name] "#{controller_class_path.join('_')}_#{singular_table_name}" + else + singular_table_name end end def plural_route_name # :doc: - if controller_class_path.empty? - plural_table_name - else + if options[:model_name] "#{controller_class_path.join('_')}_#{plural_table_name}" + else + plural_table_name end end -- cgit v1.2.3