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 fordevise
; 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
I Want to Return a Single Result Using .Find() with Ruby on Rails
Disabling Flash Message Without Disabling Cache on Click on Back Button in Rails
Ruby Devise, Sessionscontroller.Create, JSON - Getting Nameerror: Undefined 'Build_Resource'
Cannot Install Ruby 1.9.3 on a Clean Lion Install
How to Specify Rvm Gemsets for Rails Gem Whenever
Error Pushing Ruby on Rails to Heroku
Finding Parenthesis via Regular Expression
What Ruby Knowledge Should I Have
Installed Memcached via Homebrew, How to Start and Stop Server
Ruby/Rails - Active Record Db Migration to MySQL - Timestamp Type
Check If String Is Repetition of an Unknown Substring
Sorting of 2D Array by Its Amount of in the Inner Elements
How to Count the Frequencies of the Letters in User Input
Rails Undefined Method 'Errors' for Nil:Nilclass in Simple_Form
Jekyll Custom Theme- Gemspec Bundle Install Error: Unexpected Unary-, Expecting Keyword_Do