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 ?
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
Just pull your controllers in the folder.
create folder app/controllers/company
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
Just tips. For views and layout you can use: ActionController#layout and ActionController#append_view_path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With