Note that there are some explanatory texts on larger screens.

plurals
  1. POApp with Accounts and Users - Devise user can't create additional users
    primarykey
    data
    text
    <p>Using Rails 3.1.3 with Devise 1.5.3. My app has accounts and users. Each account can have multiple users. A user can have one role, "user" or "account_admin". Devise <code>signup</code> routes to <code>accounts#new</code>. The account and an initial account_admin user are created simultaneously. Got this working as described <a href="https://stackoverflow.com/questions/8305120/use-both-account-and-user-tables-with-devise/8529340#8529340">here</a> (although things have evolved some since then). </p> <p>An account_admin signs should be able to create additional users in that account. That's where I'm running into trouble: instead of creating new users, it's just redirecting to the <code>user_path</code> (users#show) page with the message "You are already signed in." (<code>user_path</code> is my overridden <code>after_sign_in_path</code>.)</p> <p>This <a href="https://stackoverflow.com/questions/4491884/rails-devise-two-different-methods-for-new-users">article</a> asks a similar question. The answer suggests a separate namespace for admins, but since admins are part of my regular app I don't think that applies.</p> <p>I've defined a complete UsersController. According to the log, <code>GET "/users/new"</code> renders from my "UsersController#new". However <code>POST "/users"</code> is intercepted by Devise and rendered from "Devise::RegistrationsController#create".</p> <p>config/routes.rb</p> <pre><code>devise_for :users devise_scope :user do get "signup", :to =&gt; "accounts#new" get "signin", :to =&gt; "devise/sessions#new" get "signout", :to =&gt; "devise/sessions#destroy" end resources :users resources :accounts </code></pre> <p>app/controllers/users_controller.rb</p> <pre><code>class UsersController &lt; ApplicationController before_filter :authenticate_user! load_and_authorize_resource # CanCan ... def new # CanCan: @user = User.new end def create # CanCan: @user = User.new(params[:user]) @user.skip_confirmation! # confirm immediately--don't require email confirmation if @user.save flash[:success] = "User added and activated." redirect_to users_path # list of all users else render 'new' end end ... end </code></pre> <p>I've tried <a href="https://stackoverflow.com/questions/3546289/override-devise-registrations-controller">overriding the Devise controller</a>, thinking I could tell it to use my <code>users#create</code> action if the user is already signed in. The log tells me it is using my controller ("Processing by RegistrationsController#create as HTML"), but it doesn't seem to execute its code. I've commented out my custom actions and just left in the logger lines, but I don't get my logging messages. And in spite of <code>super</code> being commented out, the behavior doesn't change--it still redirects with "You are already signed in."</p> <p>config/routes.rb</p> <p><code>devise_for :users, :controllers =&gt; {:registrations =&gt; "registrations"}</code></p> <p>app/controllers/registrations_controller.rb</p> <pre><code>class RegistrationsController &lt; Devise::RegistrationsController def new logger.info "Custom RegistrationsController: new" super end def create logger.info "Custom RegistrationsController: create" # super unless user_signed_in? # render "users#create" end def update super end end </code></pre> <p>What am I missing? How can I let the account_admin user create additional users?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload