Rails Controller Namespace

Rails Controller Namespace

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

Nested namespaces for controllers in Rails

The reason is simple and the answer lies in Ruby's constants resolution mechanism.

Basically it is super bad idea to have a module and class with the same name.

But if you definitely need to have same name for both module and class, be sure to correctly reference each of them.

Meaning, that referencing Manager class with :: is your only solution if you don't want to change the naming.

Rails add some magic to auto/preloading classes in development and production mode, so you could face different issues in different modes.

You may want to read through this official guide on loading constants in Rails.

Specify controller outside of namespace in Rails 6

Well, I didn't found a fix for this but I found a workaround.

Replace namespace :admin with scope '/admin/', module: :admin, as: :admin then you can use controller: '/comments' to refer controllers outside of the current namespace.

concern :commentable do
resources :comments, only: %i[create destroy], controller: '/comments'
end

How to have a namespace with action in rails?

Routes have priority in the order they are defined. So if you want /analytics/users to be handled by users#index you need to define that route first.

namespace :analytics do
get 'users', to: "users#index"
end

resources :analytics

You also don't seem to understand how nested routes work. Using resources to define nested routes would give analytics/:analytic_id/users as the index.

resources :analytics, only: [] do
resources :users, only: [:index]
end

        Prefix Verb URI Pattern                             Controller#Action
analytic_users GET /analytics/:analytic_id/users(.:format) users#index

But this is not a very restful way to setup the routes in the first place. I would suggest:

Rails.application.routes.draw do

namespace :users do
get :analytics, to: 'analytics#all'
end

resources :users, only: [] do
resources :analytics, module: :users, only: [:index]
end
end

Which would give the following routes:

         Prefix Verb URI Pattern                         Controller#Action
users_analytics GET /users/analytics(.:format) users/analytics#all
user_analytics GET /users/:user_id/analytics(.:format) users/analytics#index

The main difference here is that the routes clearly communicate that the resource being represented is analytics that belong to a user or the collection.

Using the module option sends the "nested" routes to Users::AnalyticsController and is a very good way to differentiate between different contexts.

class Users::AnalyticsController
# Display all analytics for all users
# GET /users/analytics
def all
@users = User.all
@analytics = Analytic.where(user: @users)
end

# Display all analytics for a user
# GET /users/:user_id/analytics
def index
@user = User.includes(:analytics).find(params[:user_id])
@analytics = @user.analytics
end
end

getting the namespace of a rails controller object?

I believe you are able to do params[:controller].split("/").first. This will return that namespace.

rails namespace routes and controller

I would use partials in this situation. If they are all shared in that namespace, it makes sense for the location to be the manage views directory. If they weren't namespaced but still being shared, I'd create a 'shared' directory in views.

Rails : add namespace to resource

I found a better way to do it : use module

resources :offers, module: :offers do
resources :state, only: %i(index)
end

How to generate controller inside namespace in rails

Try rails g controller admin/users if you want a users controller inside of the admin namespace. Of course, exchange users with whatever controller name that you'd like.



Related Topics



Leave a reply



Submit