Rails 3 - Devise Gem - 如何通过CRUD接口管理用户

发布于 2024-11-02 08:16:48 字数 558 浏览 1 评论 0原文

Devise gem 用于身份验证。我需要有一个用户索引和显示视图。在 devise wiki 上找到了这个如何做,但无论如何尝试,都无法使其工作。

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

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

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

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

提前致谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒人说梦 2024-11-09 08:16:48

以下内容对我有用。

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

我希望这可以帮助你。

问候,
贾科莫

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

风吹过旳痕迹 2024-11-09 08:16:48

通过将适当的 new.html.erb、edit.html.erb、_form.html.erb 等文件放入 User Views 文件夹中,添加/编辑/删除用户的工作方式应该与任何其他 CRUD 没有什么不同,因为 Devise 的 User 是另一个模型像任何一样。我不会发布新的或编辑的 erbs (足够简单,脚手架可以向您展示其余部分),但我的 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 保护用户控制器以将常规用户排除在外)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文