【问题标题】:Rails Admin Devise - How to edit other users?Rails Admin Devise - 如何编辑其他用户?
【发布时间】:2016-02-13 15:55:49
【问题描述】:

我在设计用户上创建了一个布尔值来确定管理员。现在我有了这个,我有一个用户列表。我也想让其他用户成为管理员,所以我输入了编辑链接,但他们只是继续链接到我自己的个人资料。我必须使用像 CanCan 这样的 gem,还是有办法只使用用户的布尔值来做到这一点?

admin_view.html.erb

 <div>
  <h1>Admin View</h1>
   <table>
    <thead>
     <tr>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Native Language</th>
      <th>Learning Language</th>
      <th>Admin?</th>
      <th colspan="3"></th>
     </tr>
   </thead>

   <tbody>
    <% @users.each do |user| %>
     <tr>
      <td><%= user.first_name %></td>
      <td><%= user.last_name %></td>
      <td><%#= user.meeting_time %></td>
      <td><%= user.admin %></td>
      <td><%=  %></td>
      <td><%=  %></td>
      <td><%= link_to 'Edit', edit_user_path(user) %></td>
     <% end %>
    </tr>
   </tbody>
  </table>

编辑视图

 <h2>Edit <%= resource_name.to_s.humanize %></h2>

  <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
  <%= devise_error_messages! %>

  <div class="field">
    <%= f.label :first_name %>
    <%= f.text_field :first_name, autofocus: true %>
  </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, autocomplete: "off" %>
  </div>
  <div class="form-group">
   <%= f.label :admin %>
   <%= f.check_box :admin %>
  </div>
  <div class="actions">
   <%= f.submit "Update" %>
  </div>
<% end %>

对 Rails 还是很陌生,所以如果我需要发布任何其他内容,请告诉我。谢谢!

更新

profiler_controller.rb

  def admin_view
    @users = User.all
  end

【问题讨论】:

  • 请不要在没有任何上下文的情况下对我的帖子投反对票...这对我学习没有任何帮助。
  • 我没有投反对票;原因是您基本上已经转储了一些代码并说“修复它”。 SO 上的人们会花时间,因此希望问题得到与您期望答案一样多的关注
  • 哇...我只是在问我要走的路线是否可行,或者我是否需要使用宝石。正如我所提到的,我还是新手,所以欢迎您提供反馈,但我并没有要求您“修复”任何东西。我在下面看到您建议我使用 gem,而不是继续使用布尔管理员的路线。在我继续学习一门新语言的过程中,感谢您抽出宝贵时间和对我的问题的反馈。
  • 如前所述,我没有投反对票。有人投票结束了这个问题,暗示这个问题不清楚你在问什么。

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


【解决方案1】:

这是authorization的情况(用户是否有权限)。

正如您所说的,您可以使用 CanCanCanCanCan 不再维护)来执行此操作,尽管有许多类似的 gem,例如 Pundit 等。

如果使用CanCanCan,我会使用以下内容:


#config/routes.rb
resources :users do
  post :admin #-> url.com/users/:user_id/admin
end

#app/models/ability.rb
class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new # guest user (not logged in)
    if user.admin?
      can :manage, :user
    end
  end
end

这将允许您使用:

#app/views/admin/users/index.html.erb
<% @users.each do |user| %>
  <tr>
    <td><%= user.first_name %></td>
    <td><%= user.last_name %></td>
    <td><%= user.admin %></td>
    <td><%= link_to "Admin?", user_admin_path(user), method: :post if can? :manage, user %></td>
    <td><%= link_to 'Edit', edit_user_path(user) %></td>
  </tr>
<% end %>

#app/controllers/users_controller.rb
class UsersController < ApplicationController 
  def admin
    @user = User.find params[:user_id]
    @user.toggle :admin if can? :manage, @user
    redirect_to @user 
  end
end

如果需要,我可以提供更多;不过,这应该可以回答您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 2021-11-30
    相关资源
    最近更新 更多