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 ?

2
  • 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? Commented Apr 7, 2014 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
    Commented Apr 7, 2014 at 13:19

2 Answers 2

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
6
  • 1
    Thanks guys. any drawback or complications for large scale application?
    – kashif
    Commented Apr 7, 2014 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
    Commented Apr 7, 2014 at 14:33
  • 1
    Can you explain what is the difference of those two examples?, why that two?
    – Selvamani
    Commented Nov 20, 2015 at 7:46
  • 1
    Both ways you can define Namespace, Both are same and Only changes in syntax.
    – Jenorish
    Commented Nov 20, 2015 at 10:40
  • Keep in mind, that in your example you will not be able to define Company class at the root level Commented Sep 14, 2017 at 13:15
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.

2
  • 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
    Commented Jan 12, 2017 at 19:27
  • @WilyAO that's a SourceCode Pro font I didn't remember ExtraLight or Light or Regular. Commented Jan 13, 2017 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.