2011-04-19 75 views
3

Devise gem用於認證。我需要有一個用戶索引和顯示視圖。發現這個如何在設計維基上,然而玩弄,無法使其工作。Rails 3 - Devise Gem - 如何通過CRUD界面管理用戶

Devise - How To: Manage users through a CRUD interface

方法1:請記住刪除:從設計模型登記的模塊(在我的情況下,它是用戶的模型)確保把你map.resources:在map.devise_for以下用戶:用戶路線(devise_for:用戶和資源:Rails 3的用戶)。

方法2:devise_for:用戶:串流中刪除=>「d」資源:用戶的色器件邏輯從你的CRUD用戶controlle隔離

我的問題:1。 如果您在模型中刪除登記的,則如何爲用戶設計工作? 2.如果你這樣做,你能提供一個樣本嗎?

在此先感謝

回答

3

通過將適當new.html.erb,edit.html.erb,_form.html.erb等文件,在用戶瀏覽文件夾,添加/編輯/刪除用戶應該工作無異與任何其他CRUD一樣,Devise的用戶也是其他任何模型。我不會發布新的或編輯ERBS(很簡單,並且支架可以告訴你剩下的),但我的用戶_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 %> 

在我的,我想我離開了:可註冊,我仍然可以通過CRUD管理和更新用戶。如果你不這麼做,那麼用戶不能註冊自己,但是你仍然可以自己添加和編輯用戶(當然,用load_and_authorize_resource來保護用戶控制器以保持普通用戶不在)。

+0

感謝您的幫助。讓我明白你在暗示什麼:我使用rails g devise User生成用戶模型,並且你建議這樣做:rails g controller用戶索引new show edit,右鍵? – coder 2011-04-19 20:34:21

+0

我不得不承認,我做了略微不同的地方,我有兩個用戶控制器,一個是常規users_controllers.rb,其中註冊用戶可以查看和編輯他們自己的配置文件,然後另一個admin/users_controller.rb完全用於管理員工用戶編輯後端。因爲你不想註冊,你可能只需要做一個users_controller.rb,然後把你的完整的CRUD/REST/etc放在那裏,是的。 – Shannon 2011-04-19 21:32:31

5

以下爲我工作。

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 
... 

我希望這可以幫助你。

Regards, Giacomo

+0

如果這項工作,我會upvote :) – CoderSpinoza 2013-11-05 12:25:10