【问题标题】:Rails 3 - Devise Gem - How to Manage Users through CRUD interfaceRails 3 - Devise Gem - 如何通过 CRUD 界面管理用户
【发布时间】:2011-08-08 22:35:15
【问题描述】:

Devise gem 用于身份验证。我需要有一个用户索引和显示视图。在设计 wiki 上找到了这个 how to,但是玩过,不能让它工作。

Devise - How To: Manage users through a CRUD interface

方法 1:记得从 Devise 模型中删除 :registerable 模块(在我的情况下是 User 模型)确保将 map.resources :users 放在 map.devise_for :users 路由下方(devise_for :users 和资源:Rails 3) 的用户。

方法 2:devise_for :users, :path_prefix => ‘d’ resources :users 将设计逻辑与您的 crud 用户控件隔离开来

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

提前致谢

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 devise


    【解决方案1】:

    以下对我有用。

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

    希望对你有帮助。

    问候, 贾科莫

    【讨论】:

      【解决方案2】:

      通过在 User Views 文件夹中放置适当的 new.html.erb、edit.html.erb、_form.html.erb 等文件,添加/编辑/删除用户应该与任何其他 CRUD 一样工作,因为 Devise 的用户是另一个模型。我不会发布新的或编辑的 erb(足够简单,并且脚手架可以向您展示其余部分),而是我的 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 %>
      

      在我的情况下,我想我离开了 :registerable 并且我仍然能够通过 CRUD 管理和更新用户。如果您将其省略,那么用户将无法注册自己,但您仍然可以自己添加和编辑用户(当然会使用 load_and_authorize_resource 保护用户控制器以阻止普通用户进入)。

      【讨论】:

      • 感谢您的帮助。让我理解你的建议:我使用 - rails g devise User 生成用户模型,你建议这样做:rails g controller Users index new show edit,对吗???
      • 我不得不承认,在我有两个用户控制器的情况下,我的做法略有不同,一个是普通的 users_controllers.rb,注册用户可以查看和编辑他们自己的配置文件,另一个是 admin/users_controller.rb用于管理后端中的员工用户编辑。因为你不想注册,你可能只做一个 users_controller.rb 并将你的完整 CRUD/REST/etc 放在那里,是的。
      猜你喜欢
      • 1970-01-01
      • 2011-04-22
      • 1970-01-01
      • 2011-05-05
      • 2011-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多