Reputation: 1385
I plan to make an app with different subdomains for different user types. I will namespace controllers for each of my user types. Now I am thinking what else I need to namespace to make it work right?
If I namespace controllers like this:
app/controllers/student/users_controller.erb
app/controllers/student/...
app/controllers/student/...
Then I guess I also need to namespace Views like this:
app/views/student/homeworks/index.html.erb
app/views/student/homeworks/...
app/views/student/homeworks/...
Should I also namespace helpers and my SessionController where I handle user logins? Also, I don't think I should namespace ApplicationController
so how should I handle that problem?
Thanks!
Upvotes: 0
Views: 96
Reputation: 700
The easiest way is to use genarators
rails g controller 'student/users' new create etc.
or
rails g controller student::users
When you want to add another controller:
rails g controller student::classes
This creates automatically the necessary structure for the controller and the views.
Then add in your routes:
namespace :student do
resources :users
resources :classes
end
You can use route helpers like new_student_user_path
With a non-namespaced controller you'd typically type form_for @user to create a user, with a namespaced:
<%= form_for [:student, @user] do |f| %>
...
Btw if the difference between the users is just their abilities it's better to use a gem like cancan or declarative_authorization to manage authorization. If you store different information for each type you could make a single user model with a polymorphic relationship to different profiles. More info:
How to model different users in Rails
Rails App with 3 different types of Users
......................................
Edit
You can set the layout from application_controller. I guess upon signing in you could store the layout in the cookies or the session hash: session[:layout] = 'student'
layout :set_layout
def set_layout
session[:layout] || 'application'
end
Or if you have a current_user method which gets the user you could check its class and choose the layout. I strongly advise you to reconsider splitting the user class. Different user models will lead to repeated logic and complicated authentication.
Upvotes: 1
Reputation: 1478
Without seeing the rest of your code it is difficult to know exactly what you are trying to achieve but there is a great Railscasts episode, on sub-domains in Rails, that may help you.
Namespacing your code is not essential to get your code working, although it will help you keep your code organized. Below are some additional things to think about, when working with sub-domains:
Routes
...
match '/' => 'users#show', :contraints => { :subdomain => /.+/ }
...
This example uses a constraint to route all subdomains to the users#show
action. You can use this technique to differentiate between subdomains, routing them to appropriate controller actions.
Controller
Once you have set up your routes file to route subdomains correctly, you can retrieve the subdomain in any controller via the request:
def show
@subdomain = request.subdomain
end
This will allow you to add sub-domain specific logic to your application.
View
Linking to views with other subdomains is straight forward, simply pass in the subdomain option to your url method:
root_url(subdomain: 'student')
Upvotes: 1