I'm using rails 3.2 and devise 2.1 to create a multi-site CMS
I have been searching & found few solutions
$ rails generate devise admin
$ rails generate devise author
$ rails generate devise subscriber
but this gives the following error
$ rails generate devise author
/home/gaurish/.rvm/gems/ruby-1.9.3-p286-perf/gems/devise-2.1.2/lib/devise/rails/routes.rb:443:in 'raise_no_devise_method_error!': Admin does not respond to 'devise' method. This usually means you haven't loaded your ORM file or it's being loaded too late. To fix it, be sure to require 'devise/orm/YOUR_ORM' inside 'config/initializers/devise.rb' or before your application definition in 'config/application.rb' (RuntimeError)
class Admin < User; end
class Author < User; end
class Subscriber < User; end
Here, I am not sure how this would handle different login/registration workflows. example for subscriber I am planning on using devise_invitable for creating invitations. Admin doesn't need to scoped on basis of subdomains unlike authors & subscribers.
Does this seem complicated? I hope I was able to explain well.
You don't need to have three separate models to build this functionality. What you want to look at is the concept of Roles which are applied to one User model.
There is a Gem which provides this capability called Rolify and can be found at https://github.com/EppO/rolify
This would allow you to specify which users are in which Roles and change them as you see fit, all from one existing model.
Once you have Roles attached to the User model, you can override Devise's registration controllers to detect the Role and render different templates etc. You would do this by:
rails generate devise:views
to unpack the views from the Devise gem into your projectCreate your own Registrations controller:
# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def new
super
# Add logic here to detect Role and display different forms
end
def create
super
end
def update
super
end
end
Add the correct settings in your routes.rb file to tell Devise to use your new controller:
# app/config/routes.rb
devise_for :users, :controllers => {:registrations => "registrations"}
Admin does not respond to 'devise' method.
This may be cos you're also using the activeadmin gem, or something that uses a module called Admin, which causes a name conflict. Try renaming the model to AdminUser
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