Ok, so just moved devise_for at the top of the routes file.
This is my users controller. I removed a lot of code to isolate the
problem, but even without that code, it shouldn’t tell me “user is
already sign in” and send me to the root url.
def index
@companies = Company.all(:conditions => [“account_id == ?”,
current_user.account_id])
@users = User.all(:order => ‘email’)
respond_to do |format|
format.html # index.html.erb
end
end
def show
@user = User.find(params[:id])
respond_to do |format|
format.html # show.html.erb
end
end
POST /users
POST /users.xml
def create
@user = User.new
respond_to do |format|
if @user.save
format.html { redirect_to(edit_user_path(@user), :notice =>
‘User was successfully created.’) }
format.xml { render :xml => @user, :status => :created,
:location => @user }
else
format.html { render :action => “new” }
format.xml { render :xml => @user.errors, :status =>
:unprocessable_entity }
end
end
end
def new
@user = User.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @user }
end
end
User model
belongs_to :account
belongs_to :role
belongs_to :company
has_many :tickets , :foreign_key => ‘requestor_id’ #, :class_name =>
‘Ticket’
has_many :tickets, :class_name => ‘Ticket’, :foreign_key => ‘ti’
has_many :requestors, :class_name => ‘User’, :foreign_key =>
‘requestor_id’
has_many :assignees, :class_name => ‘Ticket’
has_many :solvers, :class_name => ‘Ticket’
validates :email, :username, :first_name, :last_name, :presence =>
true, :uniqueness => true
validates :role_id, :account_id, :company_id, :presence => true
has_many :tickets, as => :assignee
has_many :tickets
Include default devise modules. Others available are:
:token_authenticatable, :encryptable, :lockable, :timeoutable and
:omniauthable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
Setup accessible (or protected) attributes for your model
attr_accessible :username, :email, :password, :password_confirmation,
:remember_me, :account_id, :company_id, :title, :last_name, :first_name
Company model
class Company < ActiveRecord::Base
has_many :users
has_one :account
validates :name, :account_id, :presence => true
end
Registration Controller
class Users::RegistrationsController < Devise::RegistrationsController
def new
end
def create
end
end
routes
devise_for :users, :controllers => { :registrations =>
‘users/registrations’ }
resources :companies
resources :users
resources :companies do
resources :users
end
new user form
company id: <%= params[:company_id] %>
<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<%= pluralize(@user.errors.count, “error”) %> prohibited this
user from being saved:
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.label :first_name %>
<%= f.text_field :first_name %>
<%= f.label :last_name %>
<%= f.text_field :last_name %>
<%= f.label :username %>
<%= f.text_field :username %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :email %>
<%= f.text_field :email %>
<% # Display password fields only if user is creating a new user %>
<% if params[:action] == ‘new’ %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
<% end %>
<%= f.submit %>
<% end %>