Zero
Zero

Reputation: 584

Rails Devise - multiple models

I need help with devise and the option to use multiple models.

At the moment i got a User model and a Client model which is inside a namespace:

class User < ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :recoverable, :registerable, :rememberable, :trackable, :validatable
end

class Test::Client < ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
end

As you can see, i included devise in both models. Like its in the doc..

So have a look at my routes:

devise_for :users, controllers: {passwords: 'passwords',
                               sessions: 'sessions'}

devise_for :clients, class_name: 'Test::Client', controllers: {sessions: 'test/sessions'}

My problem now is, that when i create a login form for my Client model:

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

it has following url: /clients/sign_in.client

What is this .client? It drives me crazy. When you need more informations let me know..

EDIT 1:

I edited my model structure now.

class User < ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :recoverable, :registerable, :rememberable, :trackable, :validatable
end

class Test::Client < User
    # some Client only functions
end

So the login as a normal User works. But the Client login still doesn't. In my view i did this:

<%= form_for(Test::Client.new, as: :client, url: session_path(:client)

When i enter my credentials i get redirected to "/users/sign_in" but nothing else happend. In my log i got a filter entry:

Filter chain halted as :require_no_authentication rendered or redirected

Upvotes: 2

Views: 640

Answers (1)

Shiva
Shiva

Reputation: 12514

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"

Upvotes: 1

Related Questions