如何通过管理员在Rails 3.2中使用Devise进行基于浏览器的新用户许可

问题描述:

我使用Rails 3.2创建了一个简单的目录,并设计了新用户在使用该站点之前需要批准的地方。我遵循中的说明如何:要求管理员在sign_in 之前激活帐户,管理员用户现在可以看到已批准用户和未批准用户的不同索引页的列表。到现在为止还挺好。

I created a simple directory using Rails 3.2 and devise where new users need approval before they can use the site. 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. So far so good.

我的问题是用户批准过程。目前我在Rails控制台中批准用户。我希望管理员用户能够通过浏览器来批准用户。我很失落我知道我需要在每个未经批准的用户旁边放置批准和不批准链接,但是什么呢?

My problem is the user approval process. Currently I approve users in the Rails console. 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.

我不知道放在我的意见和路线中。我需要一个特殊的隐藏表单,当批准提交按钮被点击并且按钮是唯一可见的元素时,只将已批准从false更改为true。

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?

如果有人可以让我从正确的方向开始,我可以从那里找出来。

If anyone can get me started in the right direction I can probably figure it out from there.

@ sas1ni69的评论引导我到 Ruby on Rails link_to使用put方法,允许我找到一个解决方案。

@sas1ni69's comment lead me to Ruby on Rails link_to With put Method which allowed me to find a solution.

对我的看法我补充说:

<%= 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!