I generated two models: Admin and User for my application, and I also generated views for each of them:
rails generate devise user
rails generate devise admin
rails g devise:views admin
rails g devise:views user
and added the next line in the devise.rb file:
config.scoped_views = true
I want to use different pages for user login and admin login, but instead the default devise login page is being called.
Thanks.
You just need to create only devise user and can override views once.
After that you have to create a different model Role to determine whether the user is admin, editor, moderator or anything else.
If you dont want Another model then you can use several gems in rails.
Rollify gem
Cancan gem
In the Above scenario, it will make devise complicated.
Edited:
To accomplish above scenario
You need to use devise_group
You need to create another model everyone
rails g devise Everyone
Now you need to define it in your Application Controller
devise_group :everyone, contains: [:user, :admin]
This might help!!!
Related
So, I have actually generated the views for devise but recently i want to change it back to another name, how will i?
Initially it was rails g devise 'Writer'...
now, i want rails g devise 'User'
Thanks.
Destroy and then generate the model.
First:
rails destroy devise Writer
then:
rails generate devise User
It would be more easier and recommended then changing.
if you cannot destroy your devise model then one method is to
rename occurrences of user_signed_in?, current_user, user_session
(see devise helpers)
Rename your db table name (migration), modify your tests files, model and route to devise_for :users.
Everything now works fine. I deleted the writer's column from the database and generated another one.
I have two models that are generated by devise: Partner and Admin. Each of them has their own sessions currents and etc. Some controllers require only Admin that logged in, some controllers require either Partner or Admin to be logged in.
There is :authenticate_admin! and :authenticate_partner! methods that will be called in my controllers before_action.
I also use CanCanCan to define both of them roles.
That gives multiple questions:
If I logged in as Partner then I opened page where Admin must be logged in and I logged in, that means that I will have two sessions at once?
I logged in as Partner when I am also Admin, that mens I need to destroy session of Admin. How to make Devise to destroy other model sessions when current model is logged in?
Do I need to add something like this in controller where or Admin or Partner is needed?
before_action :authenticate_partner!
before_action :authenticate_admin!
And the last question is: how I can make Partners open pages (that means access controllers) only that is allowed by CanCanCan ?
I wanted to use authorize_resource, it asks only one model per controller.
Yes. There are two independent devise scopes for each model, using different session variables.
Devise does not destroy a scope session when you sign in the other scope, but you can do it manually. There is devise method sign_out(scope). In your case, you can call sign_out(:partner) and sign_out(:admin) when you need it. There also must be methods like this sign_out_partner, sign_out_admin, automatically provided for your scopes by Devise. Also, pay attention to config.sign_out_all_scopes devise option.
Yes, those filters restrict access to the controller's actions.
I hope this will help How to integrate CanCan with multiple devise models?
Do you really need two models ? Maybe you can just have one model (call it User) with a role as an integer ?
class User < ActiveRecord::Base
enum role: [ :partner, :admin ]
end
I have a rails application built on top of the Devise cancan bootstrap repo.
I am trying to make a page for employee's to log hours. I set up a scaffold, and it generates a view with the form on that scaffold, but my question is how do I link to that page from the menu, and how can I make it so those logged hours are tied to whichever employee is logged in.
My scaffold is
rails g scaffold hours email:string day:date hours:integer
So besides user creation and authentication Devise has added the following
User model (backed by a users table in the database)
current_user method available in your controller code and in your views
You want to use a foreign_key user_id instead of email in all related models, your scaffold generation would be something like this (I've changed the naming slightly from yours, but you get the idea)
rails g scaffold TimeSheet user_id:integer day:date hours_worked:integer minutes_worked:integer
Then you need to update your User model and the newly created TimeSheet model to create the associations between the two models
See rails guides for more information: http://guides.rubyonrails.org/association_basics.html
class User
# ...
has_many :time_sheets
end
class TimeSheet
belongs_to :user
end
You will need to remove the user_id from the scaffold generated views and set it in the controller during create and update actions
def create
#time_sheet = TimeSheet.new(params[:time_sheet])
#time_sheet.user = current_user
# ....
I'm using rails 3.2 and devise 2.1 to create a multi-site CMS
Requirements
Sites based Basecamp subdomains.
Have 3 "user" models. 1. Admin(superuser) 2. Authors(each have their own site on subdomain) & Subscribers(read the sites ).
Authors: registration is normal username/password combo but needs to be approved by admin. their registration form will have subdomain field.
Subscribers: registration happens by invitation email.
need separate login & registration forms
Possible Solutions
I have been searching & found few solutions
3 Separate models in devise:
$ 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)
STI: single table in the database and for each user type create a model
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:
Running rails generate devise:views to unpack the views from the Devise gem into your project
Create 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
I'm using the Devise Ruby gem in my Ruby on Rails 3 application. When a logged in user creates one of my models, I want to save their user id as the person who created it. What should I do?
Create a migration file with a column of type integer named user_id, this is where the association will be stored. (see the migration guide for details on how to do this).
in your model, add: belongs_to :user (see the associations guide for more details)
in the controller for your model, add #your_model.user = current_user in the create action. This will do the association you're looking for. (current_user is a method provided by devise that returns the User ActiveRecord for the currently logged in user)
Note: there are more than one way of doing the actual association. I'm suggesting to do it in the controller but it could be done in the model or elsewhere.