【问题标题】:Creating seperate nav bars for different Devise scopes为不同的设备范围创建单独的导航栏
【发布时间】:2016-01-12 21:59:39
【问题描述】:

我的应用有三个单独的登录名:

routes.rb:

  devise_for :visitors, controllers: { registrations: 'visitor/registrations' }
  ## Visitors Authentication Scope ##
    devise_scope :visitor do
      authenticated :visitor do
        root 'properties#index', as: :visitor_authenticated_root
      end
      unauthenticated :visitor do
        root 'devise/sessions#new', as: :visitor_unauthenticated_root
      end
    end
  ## END Visitors Authentication Scope ##

  devise_for :admins, controllers: { registrations: 'admin/registrations' }
  ## Administration Authentication Scope ##
    devise_scope :admin do
      authenticated :admin do
        root 'properties#index', as: :admin_authenticated_root
      end
      unauthenticated :admin do
        root 'devise/sessions#new', as: :admin_unauthenticated_root
      end
    end
  ## END Administration Authentication Scope ##

  devise_for :residents, controllers: { registrations: 'resident/registrations' }
  ## Residents Authentication Scope ##
    devise_scope :resident do
      authenticated :resident do
        root 'properties#index', as: :resident_authenticated_root
      end
      unauthenticated :resident do
        root 'devise/sessions#new', as: :resident_unauthenticated_root
      end
    end
  ## END Residents Authentication Scope ##

目前他们都指向properties#index。管理范围将指向一个管理页面作为路由,但每个都有不同的导航栏功能和不同的视图布局。

如果我使用布尔值来设置我的用户类型,我通常会使用if/else 语句,但是我为此创建了单独的模型。

如何设置不同的布局?我读了一些文章,但没有多少是有意义的,或者没有做我想要完成的事情。

我正在使用 Rails 4 和 Ruby 2。

【问题讨论】:

    标签: ruby ruby-on-rails-4 routes


    【解决方案1】:

    只需做一些部分并使用if 语句,例如:

    if signed_in?
      if current_visitor
         = render 'path/to/visitor/navbar'
      if current_admin
         = render 'path/to/admin/navbar'
      if current_resident
         = render 'path/to/resident/navbar'
    

    【讨论】:

    • 那么这是否也可以像上面使用应用程序布局页面中的 if 语句一样工作,还是这里需要部分?我希望 Mac 栏根据用户类型(访问者、管理员或居民)扩展到整个应用程序?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-08
    • 1970-01-01
    • 2013-12-23
    • 1970-01-01
    • 2019-05-04
    • 2021-07-20
    • 2017-09-19
    相关资源
    最近更新 更多