How to redirect user after registration in apartment gem

161 views Asked by At

I am using apartment gem to achieve multitenancy. When registers on the app I want to redirect him to his own url example for localhost (xyz.localhost:3000) etc. for registration I am getting all the necessary data. a. email, b. first_name , c.last_name, d. password,e.password_confirmation ,f.subdomain

and my controller looks like:

  class Diy::AdminController < ApplicationController
     layout false

     def register
        admin  = AdminUser.find_by_email params[:email]
       unless admin 
         admin = AdminUser.create(
         first_name:params[:first_name], 
         last_name: params[:last_name],
         email: params[:email],
         password: params[:password], 
         password_confirmation:  params[:password_confirmation],
         subdomain: ( params[:subdomain] ? params[:subdomain].downcase : params[:first_name].downcase))
       end
       sign_in(admin, bypass: true)
     end
   end

I am getting Template is missing error. I want to have different home page for different user they can customize it but if they dont they will see a simple home page.

1

There are 1 answers

0
Mark On

At the moment you aren't redirecting after signing in your AdminUser. Rails therefore looks at the controller action for a template to render, and in this case there is no 'register' template namespaced under Diy/Admin.

Does each AdminUser have their own show page?

If so, I would add an attribute to each AdminUser called say 'homepage', and when the AdminUser edits their homepage then you set this attribute to true.

Then after you sign in your AdminUser, simply redirect them to either their show page or the root page depending on that attribute:

def register
   admin  = AdminUser.find_by_email params[:email]
   unless admin 
     admin = AdminUser.create(
     first_name:params[:first_name], 
     last_name: params[:last_name],
     email: params[:email],
     password: params[:password], 
     password_confirmation:  params[:password_confirmation],
     subdomain: ( params[:subdomain] ? params[:subdomain].downcase : params[:first_name].downcase))
   end
   sign_in(admin, bypass: true)
   redirect_to admin.homepage? admin : root_path
 end

You could simply remove this check and leave it at:

redirect_to admin

However this will redirect whether or not the AdminUser has made their own page. Note that

redirect_to admin

is the same as

redirect_to admin_path(admin)