Ruby on rails 如果任何型号(来自各种型号)未通过Desive验证,则重定向到特定页面

Ruby on rails 如果任何型号(来自各种型号)未通过Desive验证,则重定向到特定页面,ruby-on-rails,redirect,devise,ruby-on-rails-5,Ruby On Rails,Redirect,Devise,Ruby On Rails 5,我使用的是Rails'~>5.0.0','>=5.0.0.1',我有不同的模型Organizer和赞助商由设计4.2管理 我想要的是:如果用户未通过身份验证,请重定向到特定视图,以便他们可以单击其配置文件类型,然后重定向到相应的登录表单 实际上,当需要时,我正在使用该特性将这两个模型作为通用模型进行管理 我的文件如下: 应用程序控制器.rb class ApplicationController < ActionController::Base protect_from_forge

我使用的是
Rails'~>5.0.0','>=5.0.0.1'
,我有不同的模型Organizer赞助商
设计4.2
管理

我想要的是:如果用户未通过身份验证,请重定向到特定视图,以便他们可以单击其配置文件类型,然后重定向到相应的登录表单

实际上,当需要时,我正在使用该特性将这两个模型作为通用模型进行管理

我的文件如下:

应用程序控制器.rb

class ApplicationController < ActionController::Base
    protect_from_forgery with: :exception

    devise_group :member, contains: [:organizer, :sponsor]

    private

    def after_sign_in_path_for(resource)
        case resource
        when Organizer
            dashboard_path
        when Sponsor
            brochures_path
        end
    end
end
class BrochuresController < ApplicationController
    # Ensure an organizer is logged in before going ahead.
    before_action :authenticate_member!
end
当使用Organizer登录时,它可以正常工作

  • 新错误
当我使用赞助商登录时,我遇到以下错误:

super: no superclass method `authenticate_member!' for #<BrochuresController:0x007fa760e387f8> Did you mean? authenticate_sponsor!

Extracted source (around line #13):
11  def authenticate_member!
12      if member_signed_in?
13          super
14      else
15          redirect_to root_path, notice: "Select your profile to login!"
16      end
现在一切正常(或似乎正常)

我在谷歌上找到了
方法,在路径中有
后签名,看起来效果不错

  • 我想知道的是:
  • 我的解决方案(验证\u成员的\u路径\u中签名后)是否正确
  • 有没有其他更好的方法来解决这个问题
  • 如何在控制器操作中启用/禁用用户权限?-有些仅用于赞助商,有些仅用于组织者,有些用于两种型号

  • 顺便说一句:我找不到足够的关于
    设计组
    的信息和文档,因此如果有人对使用此功能有更好的经验,我希望您分享任何类型的文档。

    您的控制器都是从ApplicationController继承的,因此您可以轻松定义authenticate\u成员!对于每个控制器。 例如:

    # organizer_controller.rb
    def authenticate_member!
      if !member_signed_in?
        redirect_to organizers_path, notice: "Organizer notice"
      end
    end
    
    # sponsor_controller.rb
    def authenticate_member!
      if !member_signed_in?
        redirect_to sponsor_root_path, notice: "Notice for sponsors"
      end
    end
    
    NoMethodError (super: no superclass method `authenticate_member!' for #<BrochuresController:0x007fa760e387f8>
    Did you mean?  authenticate_sponsor!):
    
    app/controllers/application_controller.rb:13:in `authenticate_member!'
      Rendering /Users/alex/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
      Rendering /Users/alex/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
      Rendered /Users/alex/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (3.1ms)
      Rendering /Users/alex/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
      Rendered /Users/alex/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.7ms)
      Rendering /Users/alex/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
      Rendered /Users/alex/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.9ms)
      Rendered /Users/alex/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (143.0ms)
    
    def authenticate_member!
        if !member_signed_in?
            redirect_to root_path, notice: "Select your profile to login!"
        end
    end
    
    # organizer_controller.rb
    def authenticate_member!
      if !member_signed_in?
        redirect_to organizers_path, notice: "Organizer notice"
      end
    end
    
    # sponsor_controller.rb
    def authenticate_member!
      if !member_signed_in?
        redirect_to sponsor_root_path, notice: "Notice for sponsors"
      end
    end