Rails: Calling Devise authenticate_user! and handl

2019-06-20 16:42发布

问题:

I have a popup that will only allow to view/save some information if the user is authenticated.

I am using devise.

In the controller before_filter it checks if user is signed in and if not, show a sign in page.

This sign in page is ripped down version of the site's sign in page, so that it fits nicely to the popup.

On the authenticate action I call authenticate_user!. Everything works fine when the user enters valid credentials. But when the credential is invalid, devise automatically redirects to site's sign in page (which as I stated is different and not fit for a popup)

I tried appending a rescue to the call, but to no avail.

Anyone could suggest a better/right way to do this please? :)

def authenticate

    authenticate_user! rescue redirect_to "/popup/sign_in"

    if user_signed_in?
      respond_to do |format|
        format.html {
          flash[:notice] = I18n.t("logged_in_succesfully")
          redirect_back_or_default(accounts_path)
        }

    else
      flash[:error] = I18n.t("devise.failure.invalid")
      render "/popup/sign_in"
    end
  end