Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

[PATCH]Rails 3 : Namespace problem in Scaffold

#4763

I used the following command to generate the scaffold.
rails g scaffold admin/role name:string description:string frozen_role:boolean

I run the application it produce the routing Error( No route matches "/admin/roles" ) for the following url.
http://localhost:3000/admin/roles Then i changed the url( http://localhost:3000/roles ), it produce the Routing Error( uninitialized constant RolesController ).

Actual configuration in the routes.rb is
routes :roles But the expected is
namespace :admin do

resources :roles

end

So, I modified the routes.rb manually and check it, then also i face the problem.
I did many modification in controller and erb fils to make it work.
The problem because of using "roles" instead of "admin_roles"

-- ruby 1.8.7 (2010-01-10 patchlevel 249) [i686-linux]
Rails 3.0.0.beta3

Reported by siddick · June 4th, 2010 @ 07:02 AM

State: resolved
Milestone: 3.0.2
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. siddick
    siddick

    Actual configuration in the routes.rb is
    resources :roles

    But the expected is
    namespace :admin do

    resources :roles

    end

    June 4th, 2010 @ 07:05 AM

  2. siddick
    siddick
    • Title changed from Generate Scaffold problem to Rails 3 : Namespace problem in Scaffold

    June 4th, 2010 @ 01:04 PM

  3. siddick
    siddick
    • Tag changed from 3.0.0.beta3 to 3.0.0.beta3, patch
    • Title changed from Rails 3 : Namespace problem in Scaffold to [PATCH]Rails 3 : Namespace problem in Scaffold

    I created PATCH, which will solve the namespace problem in scaffold.

    Steps to reproduce the BUG( Rails 3.0.0.beta4, ruby 1.8.7 ) :-

    siddick@Aristo32:~$ rails new rt
    siddick@Aristo32:~$ cd rt
    siddick@Aristo32:~/rt$ rails g scaffold admin/role name:string description:string
          invoke  active_record
          create    db/migrate/20100609114623_create_admin_roles.rb
          create    app/models/admin/role.rb
          create    app/models/admin.rb
          invoke    test_unit
          create      test/unit/admin/role_test.rb
    [BUG] create      test/fixtures/admin_roles.yml
    [BUG]  route  resources :roles
          invoke  scaffold_controller
          create    app/controllers/admin/roles_controller.rb
          invoke    erb
          create      app/views/admin/roles
          create      app/views/admin/roles/index.html.erb
          create      app/views/admin/roles/edit.html.erb
          create      app/views/admin/roles/show.html.erb
          create      app/views/admin/roles/new.html.erb
          create      app/views/admin/roles/_form.html.erb
          invoke    test_unit
          create      test/functional/admin/roles_controller_test.rb
          invoke    helper
          create      app/helpers/admin/roles_helper.rb
          invoke      test_unit
          create        test/unit/helpers/admin/roles_helper_test.rb
          invoke  stylesheets
          create    public/stylesheets/scaffold.css
    
    
    siddick@Aristo32:~/rt$ rake routes
              GET    /roles(.:format)          {:controller=>"roles", :action=>"index"}
        roles POST   /roles(.:format)          {:controller=>"roles", :action=>"create"}
     new_role GET    /roles/new(.:format)      {:controller=>"roles", :action=>"new"}
              GET    /roles/:id(.:format)      {:controller=>"roles", :action=>"show"}
              PUT    /roles/:id(.:format)      {:controller=>"roles", :action=>"update"}
         role DELETE /roles/:id(.:format)      {:controller=>"roles", :action=>"destroy"}
    edit_role GET    /roles/:id/edit(.:format) {:controller=>"roles", :action=>"edit"}
    siddick@Aristo32:~/rt$ rake db:migrate
    
    siddick@Aristo32:~/rt$ rake test
    Loaded suite /home/siddick/.rvm/gems/ruby-1.8.7-p249/gems/rake-0.8.7/lib/rake/rake_test_loader
    Started
    .
    Finished in 0.119779 seconds.
    
    1 tests, 1 assertions, 0 failures, 0 errors
    Loaded suite /home/siddick/.rvm/gems/ruby-1.8.7-p249/gems/rake-0.8.7/lib/rake/rake_test_loader
    Started
    EEEEEEE
    Finished in 0.120718 seconds.
    
      1) Error:
    test_should_create_role(Admin::RolesControllerTest):
    FixtureClassNotFound: No class attached to find.
        /test/functional/admin/roles_controller_test.rb:5:in `_callback_before_33'
    
      2) Error:
    test_should_destroy_role(Admin::RolesControllerTest):
    FixtureClassNotFound: No class attached to find.
        /test/functional/admin/roles_controller_test.rb:5:in `_callback_before_33'
    
      3) Error:
    test_should_get_edit(Admin::RolesControllerTest):
    FixtureClassNotFound: No class attached to find.
        /test/functional/admin/roles_controller_test.rb:5:in `_callback_before_33'
    
      4) Error:
    test_should_get_index(Admin::RolesControllerTest):
    FixtureClassNotFound: No class attached to find.
        /test/functional/admin/roles_controller_test.rb:5:in `_callback_before_33'
    
      5) Error:
    test_should_get_new(Admin::RolesControllerTest):
    FixtureClassNotFound: No class attached to find.
        /test/functional/admin/roles_controller_test.rb:5:in `_callback_before_33'
    
      6) Error:
    test_should_show_role(Admin::RolesControllerTest):
    FixtureClassNotFound: No class attached to find.
        /test/functional/admin/roles_controller_test.rb:5:in `_callback_before_33'
    
      7) Error:
    test_should_update_role(Admin::RolesControllerTest):
    FixtureClassNotFound: No class attached to find.
        /test/functional/admin/roles_controller_test.rb:5:in `_callback_before_33'
    
    7 tests, 0 assertions, 0 failures, 7 errors
    

    After applying the PATCH :-

    siddick@Aristo32:~/rt$ 
    siddick@Aristo32:~$ rails new rt
    siddick@Aristo32:~$ cd rt
    siddick@Aristo32:~/rt$ rails g scaffold admin/role name:string description:string
          invoke  active_record
          create    db/migrate/20100609123955_create_admin_roles.rb
          create    app/models/admin/role.rb
          create    app/models/admin.rb
          invoke    test_unit
          create      test/unit/admin/role_test.rb
          create      test/fixtures/admin/roles.yml
           route  namespace :admin do resources :roles end
          invoke  scaffold_controller
          create    app/controllers/admin/roles_controller.rb
          invoke    erb
          create      app/views/admin/roles
          create      app/views/admin/roles/index.html.erb
          create      app/views/admin/roles/edit.html.erb
          create      app/views/admin/roles/show.html.erb
          create      app/views/admin/roles/new.html.erb
          create      app/views/admin/roles/_form.html.erb
          invoke    test_unit
          create      test/functional/admin/roles_controller_test.rb
          invoke    helper
          create      app/helpers/admin/roles_helper.rb
          invoke      test_unit
          create        test/unit/helpers/admin/roles_helper_test.rb
          invoke  stylesheets
          create    public/stylesheets/scaffold.css
    
    
    siddick@Aristo32:~/rt$ rake routes
                    GET    /admin/roles(.:format)          {:controller=>"admin/roles", :action=>"index"}
        admin_roles POST   /admin/roles(.:format)          {:controller=>"admin/roles", :action=>"create"}
     new_admin_role GET    /admin/roles/new(.:format)      {:controller=>"admin/roles", :action=>"new"}
                    GET    /admin/roles/:id(.:format)      {:controller=>"admin/roles", :action=>"show"}
                    PUT    /admin/roles/:id(.:format)      {:controller=>"admin/roles", :action=>"update"}
         admin_role DELETE /admin/roles/:id(.:format)      {:controller=>"admin/roles", :action=>"destroy"}
    edit_admin_role GET    /admin/roles/:id/edit(.:format) {:controller=>"admin/roles", :action=>"edit"}
    
    
    siddick@Aristo32:~/rt$ rake db:migrate
    siddick@Aristo32:~/rt$ rake test
    Loaded suite /home/siddick/.rvm/gems/ruby-1.8.7-p249/gems/rake-0.8.7/lib/rake/rake_test_loader
    Started
    .
    Finished in 0.180626 seconds.
    
    1 tests, 1 assertions, 0 failures, 0 errors
    Loaded suite /home/siddick/.rvm/gems/ruby-1.8.7-p249/gems/rake-0.8.7/lib/rake/rake_test_loader
    Started
    .......
    Finished in 0.301821 seconds.
    
    7 tests, 10 assertions, 0 failures, 0 errors
    

    Testing :-

    I added testcase to test the namespace problem in scaffold.

    rake test:regular TEST=test/generators/scaffold_generator_test.rb
    

    June 10th, 2010 @ 11:13 AM

  4. siddick
    siddick
    • Tag changed from 3.0.0.beta3, patch to rails 3.0.0.beta4, patch, scaffold

    Any Updates on this bug..

    June 18th, 2010 @ 12:25 PM

  5. José Valim
    José Valim
    • Milestone cleared.
    • State changed from new to open
    • Assigned user set to José Valim

    Hey mate, thanks for the awesome patch with tests! However, it needs to be rebased, could you do it? (Sorry for not being able to see it earlier)

    Also, I have one suggestion. I noticed you changed all orm_instance calls to orm_instance(singular_table_name). Wouldn't it be better if you simply change the default value given to orm_instance instead:

    http://github.com/rails/rails/blob/master/railties/lib/rails/genera...

    It could use singular_table_name instead of file_name.

    June 21st, 2010 @ 10:36 AM

  6. José Valim
  7. siddick
    siddick

    I attached the New Patch, with the changes that you have suggested.

    June 23rd, 2010 @ 08:22 AM

  8. José Valim
    José Valim

    Sweet! Thanks for the great work!

    June 23rd, 2010 @ 08:32 AM

  9. Repository
  10. siddick
    siddick

    Thanks for all your support..

    June 23rd, 2010 @ 08:40 AM

  11. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to Low

    October 15th, 2010 @ 11:01 PM