Rails 3 - Devise Gem - How to Manage Users through CRUD interface

后端 未结 2 1156
误落风尘
误落风尘 2020-12-19 23:05

Devise gem is used for authentication. I need to have a user index and show view. Found this how to at the devise wiki, however played around, can not make it work.

相关标签:
2条回答
  • By placing the appropriate new.html.erb, edit.html.erb, _form.html.erb etc files in the User Views folder, adding/editing/deleting Users should work no differently as any other CRUD, as Devise's User is another model like any. I won't post the new or edit erbs (simple enough, and a scaffold can show you the rest), but an example of my User _form.html.erb...

    <%= form_for(@user) do |f| %>
       <%= render :partial => 'shared/errors', :locals => { :content => @user } %>
    
       <div class="field">
          <%= f.label :email %><br />
          <%= f.text_field :email %>
       </div>   
    
       <div class="field">  
          <%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
          <%= f.password_field :password %>
       </div>
    
       <div class="field">
          <%= f.label :password_confirmation %><br />
          <%= f.password_field :password_confirmation %>
       </div>
    
       <div class="field">
          <%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
          <%= f.password_field :current_password %>
       </div>
    
       <div class="actions">
          <%= f.submit %>
       </div>
    
    <% end %>
    

    In mine, I think I left :registerable in and I'm still able to administer and update users via the CRUD. If you leave it out, then users can't register themselves, but you can still add and edit users yourself (would protect the Users controller with load_and_authorize_resource to keep regular users out, of course).

    0 讨论(0)
  • 2020-12-19 23:39

    The following worked for me.

    users_controller.rb

    class UsersController < ApplicationController
    ...
      def create
        @user = User.new(params[:user])
    
        if params[:user][:password].blank?
          params[:user].delete(:password)
          params[:user].delete(:password_confirmation)
        end
    
        respond_to do |format|
          if @user.save
            format.html { redirect_to users_path, notice: 'User was successfully created.' }
          else
            format.html { render action: "new" }
          end
        end
      end
    ...
    end
    

    routes.rb

    ...
    devise_for :users, :path_prefix => 'd'
    resources :users
    ...
    

    I hope this can help you.

    Regards, Giacomo

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