Using Devise for More Than One Models in Rails App

Rails Devise - multiple models

Answer to the original question

in my app

<%= user_session_path(resource_name) %>
# Output
/users/sign_in.user
# In this example; the `resource_name` is redundant

<%= user_session_path %>
# no need to pass arg is your are surely gonna use `user` as the resource
# Output
/users/sign_in

<%= session_path(:user) %>
# Output
/users/sign_in

there is a helper method defined in devise to determine the sign_in_url according to resource;

What is this .client?
Ans: client is the name of the resource you defined for devise; in your case there are two resources;

use the following if you are gonna have different view templates for different resources;

<%= form_for(resource, as: resource_name, url: client_session_path, html: {class: ''}) do |f| %>
<% end %>

If want to share the view template; use

<%= form_for(resource, as: resource_name, url: session_path(resource_name), html: {class: ''}) do |f| %>
<% end %>

FYI :

[8] pry(main)> app.members_path
=> "/members"
[9] pry(main)> app.leads_path(2)
=> "/leads.2"
[10] pry(main)> app.lead_path(2)
=> "/leads/2"

Using devise for more than one models in rails app

Devise can work for you, but you will have to run through some tutorials. Your request for a complete how-to is too broad for an Stack Overflow question. http://railsapps.github.io/tutorial-rails-devise-rspec-cucumber.html#authorization

You may also want to look at a gem to manage permissions, as Devise is more for authentication. My personal favorite is the_role, but others like cancan very much.

More than one type of user using devise gem

Use one model but have multiple roles (teacher, student, etc...) for it with something like rolify. This will be a lot leaner and more flexible down the line than having distinct controllers, models and so on.

Using Devise for Two Different Models but the same Login Form

I think the only way to handle this would be to have your own custom sign in form and controller that determined the type of user and then sign them in correctly. I would recommend an approach like what mark mentioned for simplicity (take a look at something like CanCan to manage roles).

Another potential problem with having multiple user models is that you will have multiple versions of all the devise helper methods. So for current_<resource> and <resource>_signed_in? you would have current_user, current_business_user, user_signed_in? and business_user_signed_in?. Then you would either have to implement your own versions of these methods or you would need to check both versions everywhere you used them.

Devise Views with Multiple Models

I solved this with the help from another post Override Devise Registrations Controller Two Times? I found that I needed to create a directory within my "app/controllers" directory. This is how you use the scoped controller:

"app/controllers/admin/registrations_controller.rb"

class Admin::RegistrationsController < Devise::RegistrationsController
#Your Code Here
end

I can leave my first controller and keep it for the users "app/controllers/registrations_controller.rb"

class RegistrationsController < Devise::RegistrationsController
#Your Code Here
end

My "config/routes.rb" will look like:

devise_for :admins, :controllers => {:registrations => "admins/registrations"}
devise_for :users, :controllers => {:registrations => "registrations"}


Related Topics



Leave a reply



Submit