且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Rails 3 - Devise Gem - 如何通过CRUD界面管理用户

更新时间:2022-10-20 20:40:38

通过在User Views文件夹中放置相应的new.html.erb,edit.html.erb,_form.html.erb等文件,添加/编辑/删除用户的工作方式与任何其他CRUD,因为Devise的用户是另一个类似的模型。我不会发布新的或编辑的呃(简单,脚手架可以显示你的休息),但我的用户_form.html.erb ...

 <%= form_for(@user)do | f | %GT; 
<%= 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>(如果不想更改则为空)< / 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(我们需要您当前的密码来确认您的更改)< / i>< br />
<%= f.password_field:current_password%>
< / div>

< div class =actions>
<%= f.submit%>
< / div>

<%end%>

在我的,我想我离开了:可注册,我仍然可以管理和更新用户通过CRUD。如果你把它留下来,那么用户不能自己注册,但你仍然可以自己添加和编辑用户(当然,可以使用load_and_authorize_resource保护用户控制器,以保持普通用户的安全。)


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.

Devise - How To: Manage users through a CRUD interface

Method 1: Remember to remove the :registerable module from the Devise model (in my case it was the User model) Make sure to put your map.resources :users below the map.devise_for :users route (devise_for :users and resources :users for Rails 3).

Method 2: devise_for :users, :path_prefix => ‘d’ resources :users to isolate the devise logic from your crud user controlle

My question: 1. if you remove registerable in the model, then how does devise work for user? 2. If you done this, can you provide a sample?

Thanks in advance

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