0

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!

user3339562
  • 1,325
  • 4
  • 18
  • 34

2 Answers2

1

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')
Tom Kadwill
  • 1,448
  • 3
  • 15
  • 21
  • Thanks but I am trying to avoid a situation where I will have a lot of 'if' statements inside my views and controllers. My users have much different view when they are looking for example 'show' view for 'Homework'. I will have 4 types of users and I don't want messy code. That's why it wont be DRY but big advantage would be much cleaner code and much easier to maintain. I am just not sure how to handle those doubts inside my question. – user3339562 Sep 13 '14 at 15:30
1

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.

Community
  • 1
  • 1
  • Thank you, +1 :). Can you just advise me how can I handle layouts in this case. I have one layout folder inside my view folder where I have application view in which I show layout depending on which user is logged ib. Should I also create layout folder inside 'student' namespace folder? Thanks! – user3339562 Sep 13 '14 at 16:56