简体   繁体   English

如何使用Devise在Rails 3.2中通过Admin进行基于浏览器的新用户批准

[英]How to do browser-based new user approval by Admin in Rails 3.2 using Devise

I created a simple directory using Rails 3.2 and devise where new users need approval before they can use the site. 我使用Rails 3.2创建了一个简单的目录,并设计了新用户在使用该站点之前需要获得批准的地方。 I followed the instructions in " How To: Require admin to activate account before sign_in " and admin users can now see lists of different index pages of approved versus non-approved users. 我按照“ 如何:需要管理员在sign_in之前激活帐户 ”中的说明进行操作,并且管理员用户现在可以看到已批准和未批准用户的不同索引页的列表。 So far so good. 到现在为止还挺好。

My problem is the user approval process. 我的问题是用户批准过程。 Currently I approve users in the Rails console. 目前,我在Rails控制台中批准用户。 I'd like for admin users to be able to approve users through their browser. 我希望管理员用户能够通过其浏览器批准用户。 I'm at a loss. 我很茫然。 I know I need to put "approve" and "don't approve" links next to each unapproved user but then what? 我知道我需要在每个未批准的用户旁边放置“批准”和“不批准”链接,但是那又是什么呢?

In the abstract I know that clicking those links should activate a method in the User model or controller and then redirect with flash but I've strayed beyond what I know from my beginner tutorials. 在摘要中,我知道单击那些链接应该会激活User模型或控制器中的方法,然后使用Flash进行重定向,但是我偏离了我的初学者教程中所知道的范围。

I'm not sure what to put in my views and routes. 我不确定要在视图和路线中添加什么。 Do I need a special hidden form that only changes 'approved' from false to true when when the "approve" submit button is clicked and the button is the only visible element? 我是否需要一种特殊的隐藏形式,当单击“批准”提交按钮并且该按钮是唯一可见的元素时,该表单仅将“批准”从false更改为true?

If anyone can get me started in the right direction I can probably figure it out from there. 如果有人可以引导我朝着正确的方向发展,我可能可以从那里弄清楚。

@sas1ni69's comment lead me to Ruby on Rails link_to With put Method which allowed me to find a solution. @ sas1ni69的评论将我带到Ruby on Rails link_to和put方法 ,这使我能够找到解决方案。

To my view I added: 我认为,我补充说:

<%= link_to "approve", approve_user_path(user.id)  %>

To my routes I added: 在我的路线中,我添加了:

match 'users/:id/approve'=> 'users#approve_user', as: 'approve_user'

To my user controller I added: 在我的用户控制器中,我添加了:

def approve_user
  user = User.find(params[:id])
  user.approved = true
  if user.save
    flash[:notice] = "#{user.full_name} approved"
  else
    flash[:alert] = "#{user.full_name} approval failure"
  end
  redirect_to :back
end

Seems to be working like a charm! 似乎在工作,就像一种魅力! Thanks everybody! 谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM