I am trying to create a rails application Shopper with three models: Category, Items and Buyers. Each category can have many items(has_many) and each buyer can buy many items(has_many). This application needs a login page for users. I have installed ActiveAdmin and Devise gems installed for my application(Shopper). How to create a form for login, logout and signup with these gems. If the signed in user matches with the admin name and password it must show the admin panel(Active Admin) where you can create, update and delete a Category and Item and if the signed in user is not admin(a Buyer) it must show the Items page in the application where the Buyer can choose items. How can I do this?
I tried this How do I use Devise and ActiveAdmin for the same User model? But it didn't apply for my application.
While it is possible to authenticate different models with Devise, the most common solution to your situation would be to have one model names User
for both your buyers and your admin(s). In the underlying database table, you then add a boolean column admin
for which ActiveRecord then will automatically provide an admin?
method on the User
model.
Then it is just a matter of having one root
route (in your config/routes.rb
), something like application#welcome
. In that controller action you then can check if current_user.admin?
and redirect_to
either your admin panel or your items index page.