Rails engine and devise

2019-04-11 08:15发布

问题:

I have a Rails Engine, where I want to use Devise. I installed devise like normal following this guide.

I added this in my engine devise.rb:

Devise.setup do |config|
  config.router_name = :cms_user
end

I added this in my routes file:

Cms::User::Engine.routes.draw do
  devise_for :users, {
    class_name: 'Cms::User',
    module: :devise
  }
end

I added this in my routes:

devise_for :users, {
  class_name: 'Cms::User',
  module: :devise
}

However I keep getting this error:

undefined method 'cms_user'

What am I doing wrong ?

回答1:

Here's the how to:

  1. Rails plugin new cms --mountable -d postgresql

2: Install devise like normal

3: Add this in the main application routes file:

  devise_for :users, {
     class_name: 'Cms::User',
     module: :devise
   }

This line here, mentioned in the devise wiki, caused the problems for me:

config.router_name = :Cms_user 

Wiki: https://github.com/plataformatec/devise/wiki/How-To:-Use-devise-inside-a-mountable-engine