【问题标题】:Rails 4 - Using namespaced STI models with deviseRails 4 - 在设计中使用命名空间的 STI 模型
【发布时间】:2015-10-30 08:05:09
【问题描述】:

我的 rails 应用有 5 个扩展 User 的用户模型。所有这些都具有命名空间User::UserTypeHere。当我尝试注销我的应用程序时,devise 尝试访问模型,就好像它们属于顶级命名空间一样,我收到以下错误:

"The single-table inheritance mechanism failed to locate the subclass: 'SuperUser'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance..."

如何以设计能够识别我的命名空间模型的方式设置我的路由?

routes.rb sn-p

...
devise_for :users, skip: [:sessions, :passwords, :confirmations, :registrations, :unlocks]
devise_scope :user do

# authentication
unauthenticated :user do
  root to: 'users/devise/sessions#new', as: 'new_user_session'
end
authenticated :user do
  root to: 'application#index'
end

get '/login', to: 'users/devise/sessions#new', as: 'user_login_view'
post '/login', to: 'users/devise/sessions#create', as: 'user_session'
get '/logout', to: 'users/devise/sessions#destroy', as: 'destroy_user_session'

# registrations
get '/join', to: 'users/registrations#new', as: 'new_user_registration'
post '/join', to: 'users/registrations#create', as: 'user_registration'

# user accounts
scope '/account' do
  # confirmation
  get '/verification', to: 'users/confirmations#verification_sent', as: 'user_verification_sent'
  get '/confirm', to: 'users/confirmations#show', as: 'user_confirmation'
  get '/confirm/resend', to: 'users/confirmations#new', as: 'new_user_confirmation'
  post '/confirm', to: 'users/confirmations#create'

  # passwords
  get '/reset-password', to: 'users/passwords#new', as: 'new_user_password'
  get '/reset-password/change', to: 'users/passwords#edit', as: 'edit_user_password'
  put  '/reset-password', to: 'users/passwords#update', as: 'user_password'
  post '/reset-password', to: 'users/passwords#create'

  # unlocks
  post '/unlock', to: 'users/unlocks#create', as: 'user_unlock'
  get '/unlock/new', to: 'users/unlocks#new', as: 'new_user_unlock'
  get '/unlock', to: 'users/unlocks#show'

  # settings & cancellation
  # get '/cancel', to: 'users/registrations#cancel', as: 'cancel_user_registration'
  # get '/settings', to: 'users/registrations#edit', as: 'edit_user_registration'
  # put '/settings', to: 'users/registrations#update'
  # account deletion
  # delete '', to: 'users/registrations#destroy'
end
end
...

user.rb(模型)

class User < ActiveRecord::Base
    devise :database_authenticatable, :registerable,
        :recoverable, :rememberable, :trackable, :validatable
end

users/super_user.rb(模型)

class Users::SuperUser < User
    ...
end

users/devise/sessions_controller.rb

class Users::Devise::SessionsController < Devise::SessionsController
    ...
end

【问题讨论】:

    标签: ruby-on-rails ruby devise model sti


    【解决方案1】:

    您的用户的type 列的值需要为Users::SuperUser,而不仅仅是SuperUser,以便Rails 自动加载可以找到正确的类路径。

    另外,请记住在更改类时重新启动您的 Rails 控制台和服务器,并且不要让旧用户实例的类型值错误,因为这会迫使 Rails 在找到它们时触发错误

    【讨论】:

      猜你喜欢
      • 2019-02-22
      • 1970-01-01
      • 2013-02-12
      • 2013-04-18
      • 2013-09-26
      • 2012-09-10
      • 2016-06-17
      • 2012-04-18
      • 2017-03-21
      相关资源
      最近更新 更多