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

前端 未结 4 1658
深忆病人
深忆病人 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: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' %>
        

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

    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.

提交回复
热议问题