【问题标题】:Devise Session Controller. Custom routing on failure设计会话控制器。失败时的自定义路由
【发布时间】:2016-10-26 00:20:24
【问题描述】:

现在我正在扩展设计会话控制器。一切正常,除了密码输入错误或找不到用户。它试图重定向到会话#new。对于我的情况,我不希望它重定向到自定义路由,因为它来自与会话新视图不同的视图。我知道after_sign_in_path_for,但我不确定这是我想要的,因为我的特殊情况是warden.authenticate!方法。在 auth_options 中有一个召回哈希。那就是我要自定义路线的地方。这是我的会话控制器。

会话控制器

    class SessionsController < Devise::SessionsController
  def create

    super do |user|
      if params[:user][:invitation_id].present?
        invitation = Invitation.find(params[:user][:invitation_id])

        if !user.accounts.exists?(id: invitation.account.id)
          user.accounts << invitation.account
          flash[:tip_off] = "You now have access to your project \"#{invitation.account.name}\""
        else
          flash[:tip_off] = "You are already a member of #{invitation.account.name}"
        end

        cookies[:account_id] = invitation.account.id
        invitation.destroy
        user.save
      end
    end
  end
end

正如我之前所说,这很好用,我只需要在密码或电子邮件失败时自定义路由。现在它转到会话#new,这对我的情况不起作用。任何帮助都会很棒!谢谢

【问题讨论】:

    标签: ruby-on-rails ruby session devise


    【解决方案1】:

    您可以覆盖 auth_option 方法上的调用值,以便在失败时重定向到另一个路由。

    def auth_option
      { scope: resource_name, recall: "#{controller_path}#another_new_path" }
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-26
      相关资源
      最近更新 更多