Allow a user to add new users in Devise, and remain logged in as themselves

前端 未结 4 1652
深忆病人
深忆病人 2020-12-08 23:45

I am using Ruby On Rails with Devise, Rails 4.1.0.rc1, Ruby 2.1.0p0, devise-3.2.4

I followed the tutorial from Rails Cast Episode #209 to get devise installed and wo

相关标签:
4条回答
  • 2020-12-09 00:14

    I think the problem is actually with your routes. I imagine that you've added resources :users to your routes file to set up routes for your UsersController. However, I'm guessing that you've also got devise_for :users … to set up devise. This means that you'll have devise and your UsersController fighting for the same routes. Specifically, you're expecting a POST to /users to create a new user, but it's actually routing to devise's RegistrationsController and registering a new user and signing them in.

    You can see this by running rake routes and seeing that both devise and your UsersController have, for example, mappings for POST /users(.:format). You need to separate out the two sets of routes. There are various ways you can do this. You could add :path => 'u' to your devise_for line in routes.rb, which will then mean your devise routes are all /u instead of /users. Alternatively, you could keep devise routes on /users and instead change your UsersController routes to something like:

    resources :users_admin, :controller => 'users'
    

    which will map /users_admin to your UsersController (but note the path helpers will also change from e.g. users_path to users_admin_path).

    0 讨论(0)
  • 2020-12-09 00:17

    As i understood your question(text part, not read the code), you want users to add other users and the accounts should be ready to be used. TO register using devise, you would be using user email, password and password_confirmation field as mandatory.

    When registering users with email, provide a dummy password to the accounts so that the user entry is being saved. Send mails to the users to confirm their account and change password using the password reset links.

    0 讨论(0)
  • 2020-12-09 00:28

    Here is what I finally did to solve this

    in routes.rb

      devise_for :users  
      resources :users_admin, :controller => 'users'
    

    in users_controller.rb no changes

    in form.html.erb for adding new & updating users. Note the specified URL. This is documented to some degree here, but only mentions it for singular resources http://guides.rubyonrails.org/routing.html

    However I had to separate out the edit & new paths as the url would not work in both create & update at the same time, so instead of rendering a partial, I just have 2 forms. Not sure what the workaround is.

    This one is my new user form:

    <%= form_for @user, url: users_admin_index_path(@user)  do |f| %> 
    

    And this one is in my edit user form:

    <%= form_for @user, url: users_admin_path(@user)  do |f| %>  
    

    At the bottom of the form I have this code:

     <% if @user.new_record? %>
            <%= f.submit "Create Account" %>
          <% else %>
            <%= f.submit "Update Account" %>
          <% end %>
          <%= link_to "Cancel", users_admin_index_path, class: 'button' %>
        </p>
    

    Then in the show.html.erb form I had to specify the URLS from 'rake routes'

    <footer>
      <nav>
        <%= link_to "Edit User", edit_users_admin_path, class: 'button' %> |
        <%= link_to "New User", new_users_admin_path, class: 'button' %> | 
        <%= link_to "All Users", users_admin_index_path, class: 'button' %>
      </nav>
    </footer>
    

    I got the path names from rake routes.

    I hope this helps somebody else out and thank you all for the help. I do not have enough reputation to upvote any answers yet.

    0 讨论(0)
  • 2020-12-09 00:35

    GhostRider is correct on the workflow. You could also move the logic to an AdminsController or similar. This thread will solve the specific issue of getting signed in as that user: Devise: How to create a new user being already logged in?

    But it is a better workflow to have the admin set a temporary password and email the new user to have them finish creating their account.

    0 讨论(0)
提交回复
热议问题