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



为鉴权方式。我需要有一个用户索引和显示视图。在设计的wiki上发现了这个如何,但是玩了一下,不能使它工作。

design -如何:通过CRUD接口管理用户

方法1:记住从设计模型中删除:registerable模块(在我的例子中是User模型)。

资源:地图下面的用户。

方法2:device_for:users,:path_prefix => ' d ' resources:users将设计逻辑与您的crud用户控件隔离

我的问题:1. 如果您在模型中删除了可注册的,那么设计如何为用户工作?2. 如果你这样做了,你能提供样品吗?

Thanks in advance

下面的方法对我很有效。

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

我希望这对你有帮助。

问候,Giacomo

通过放置适当的new.html。edit.html erb。_form.html erb。添加/编辑/删除用户应该与任何其他CRUD没有什么不同,因为设计的用户是另一个模型。我不会发布新的或编辑的动词(很简单,并且脚手架可以向您展示其余部分),但是我的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 %>

在我的系统中,我认为我留下了:可注册的,并且我仍然能够通过CRUD管理和更新用户。如果省略它,那么用户就不能注册自己,但您仍然可以自己添加和编辑用户(当然,将使用load_and_authorize_resource保护users控制器,以将普通用户排除在外)。

最新更新