16

What are advantages and disadvantages of using namespace in ruby on rails. For example: I've many controllers like

CompanyLocations 
CompanyXXXX 
CompanySports 
CompanyActivites
CompanyQQQQQ

I want to put all these controllers in Company folder. What is the rails best practice for this ?

kashif
  • 1,097
  • 4
  • 17
  • 32
  • 1
    What's the wider context of your application? How many other non-company related tables are there? Do each of these cases really need to be prefixed with 'Company'? Or is 'Company' kind of implied when you take into account the whole application? – joshua.paling Apr 07 '14 at 13:07
  • 1
    Its a large scale application. For some reason, I need to keep companies as prefix in many controllers. Question is not specific to any application. i just like to know the best practice and drawbacks, if we put all similar controllers in one folder in large apps. – kashif Apr 07 '14 at 13:19

2 Answers2

37

You have to create a subfolder inside your controller/ directory, and the same in your views/ directory.

Your controller file should look like

module Company
 class SportsController < ApplicationController

 def index
 end

 end
end

...or

class Company::SportsController < ApplicationController

 def index
 end

end

You can also call your partials this way

render :template => "company/sports/index"

Then in routes.rb

namespace :company do
 resources :sports
end
Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164
Jenorish
  • 1,694
  • 14
  • 19
  • 1
    Thanks guys. any drawback or complications for large scale application? – kashif Apr 07 '14 at 14:06
  • 2
    No problem,using name scope good only but be careful to call the API and routes and views that's it – Jenorish Apr 07 '14 at 14:33
  • 1
    Can you explain what is the difference of those two examples?, why that two? – Selvamani Nov 20 '15 at 07:46
  • 1
    Both ways you can define Namespace, Both are same and Only changes in syntax. – Jenorish Nov 20 '15 at 10:40
  • Keep in mind, that in your example you will not be able to define `Company` class at the root level – Artur INTECH Sep 14 '17 at 13:15
  • Is there a difference between the first and second examples of the controller file? i.e., is module Company class SportsController < ApplicationController any different from class Company::SportsController < ApplicationController? – nimmolo May 05 '20 at 00:22
26

Just pull your controllers in the folder.
create folder app/controllers/company
enter image description here
and create a controller locations_controller.rb with structure:

module Company
  class LocationsController < ApplicationController
    layout '/path/to/layout'
    append_view_path 'app/views/path/to/views'

    def index
    end

  end
end

in routes.rb use scope :module:

scope module: 'company' do
  get '/locations', to: 'locations#index' # this route in scope
end

this generate routes:

locations_path   GET     /locations(.:format)    company/locations#index

update:

Just tips. For views and layout you can use: ActionController#layout and ActionController#append_view_path.

Roman Kiselenko
  • 43,210
  • 9
  • 91
  • 103
  • Sorry it is not referring to the issue is as to the source of the font text would like to know what it is. Sorry I do not speak or write very well English – Wily AO Jan 12 '17 at 19:27
  • @WilyAO that's a `SourceCode Pro` font I didn't remember `ExtraLight` or `Light` or `Regular`. – Roman Kiselenko Jan 13 '17 at 10:38