[英]devise admin user approval setup
Seems like the tutorial located here has a couple omissions: 似乎这里的教程有两个遗漏之处:
When creating the admin accessible controller method, they don't specify which custom devise controller to use, or which base controller to inherit from. 创建管理员访问控制方法时,他们并不指定哪些定制设计控制器使用,或以继承其基本控制器。 So I've placed my code in a PagesController:
因此,我将代码放置在PagesController中:
class PagesController < ApplicationController def approve_users if current_user.admin? if params[:approved] == "false" @users = User.find_by_approved(false) else @users = User.all end end end end
The view code that allows you to switch between all users and all unapproved users results in a NoMethodError in Pages@approve_users
: undefined method 'each' for User
whenever you select to show the users for whom :approved => false
. 允许您在所有用户和所有未批准用户之间切换的视图代码会在您选择显示其
:approved => false
undefined method 'each' for User
时NoMethodError in Pages@approve_users
: undefined method 'each' for User
NoMethodError in Pages@approve_users
。 I know why noMethodErrors spring up in app development, and would normally be able to wrap my head around why I'm getting this error. 我知道为什么noMethodErrors会在应用程序开发中兴起,并且通常能够使我神清气爽地理解为什么收到此错误。 It works when
@users = User.all
, but not when @users = User.find_by_approved(false)
它的工作原理 ,当
@users = User.all
,而不是在@users = User.find_by_approved(false)
<% if current_user.admin? %> <h2>Users</h2> <%= link_to "All Users", :action => "approve_users" %> | <%= link_to "Users awaiting approval", :action => "approve_users", :approved => "false" %> <div class="ui form"> <table> <thead> <tr scope="col"> <th>First name</th> <th>Last name</th> <th>E-mail</th> <th>Approve</th> </tr> </thead> <% @users.each do |user| %> <tbody> <tr> <td><%= user.firstname %></td> <td><%= user.lastname %></td> <td><%= user.email %></td> <td class="ui checkbox"> <input type="checkbox" tabindex="0" class="hidden"> </td> </tr> </tbody> <% end %> </table> </div> <% end %>
I made a custom admin controller specifically for an admin control panel and put all of the tools in an index page. 我专门为管理控制面板制作了一个自定义管理控制器,并将所有工具放在索引页面中。 You can probably do it a number of ways, though.
不过,您可能可以通过多种方式来执行此操作。
The common consensus on this seems to be that if you switch @users = User.find_by_approved(false) to @users = User.where(approved: false), it works better. 对此的普遍共识似乎是,如果将@users = User.find_by_approved(false)切换为@users = User.where(approved:false),则效果更好。 That's what I currently have and it works very well.
这就是我目前拥有的,并且效果很好。
I had this problem as well, and I ended up scrapping devise and making a custom user sign in method. 我也遇到了这个问题,最终我放弃了设计并制作了自定义用户登录方法。 You should be able to make it work, though.
不过,您应该能够使其工作。 I followed the tutorial here and it helped immensely.
我在这里遵循了该教程,对它有极大的帮助。 Basically, you want to create a method which will approve users in your admin controller.
基本上,您想创建一种方法来批准您的管理控制器中的用户。 This is the one I used:
这是我使用的一个:
` `
def approve
User.where(id: params[:user_id]).update_all(approved: true)
redirect_to admin_index_path
end
From there, you add a put method to your routes. 从那里,您将put方法添加到路由中。
put 'approve_admin', to: "admin#approve", as: :approve_admin
Finally, wrap your list of users in a form tag and add a hash of all of the user IDs you want to update as a hash. 最后,将您的用户列表包装在一个表单标签中,然后将您要更新的所有用户ID的哈希添加为哈希。
<%= form_tag(approve_admin_path, method: :put) do %>
<% for user in @unapproved_users %>
<tr>
<td class="mdl-data-table__cell--non-numeric"><%= check_box_tag "user_id[]", user.id %></td>
<td><%= user.name %></td>
<td><%= user.email %></td>
</tr>
<% end %>
</tbody>
</table>
<%= submit_tag "Mark as approved", class: 'mdl-button mdl-js-button mdl-button--raised approve' %>
<% end %>
I made a method for approving users and another for unapproving users so I switched up the @users.each iteration in favor of a for iteration. 我制定了一种批准用户的方法,另一种批准用户的方法,所以我将@ users.each迭代切换为for迭代。 I added @unapproved_users to my index method to make this work properly.
我在索引方法中添加了@unapproved_users,以使其正常工作。 Hope this works for you!
希望这对您有用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.