Rails, how to post a form to Devise?

2019-06-13 01:57发布

问题:

I want to allow an admin on my app to sign in as any user. I found the following on the devise wiki:

class AdminController < ApplicationController
  # Sign in as another user if you are an admin
  def become
    return unless current_user.is_an_admin?
    sign_in(:user, User.find(params[:id]))
    redirect_to root_path
  end
end

In the view, how do you build a form to post to this?

Thanks

回答1:

You wouldn't even need to build a form, you could simply allow the admin to go to the following url:

example.com/admin/become?id=25

where 25 is the id you want to log into as.

So with this method, you would just create a link for an admin to click on.