ruby-on-railsrubydevisealchemy-cms

Devise controller method not found using Alchemy CMS


I have a Rails 4.1.8 app which was developed by a third party; it already uses Devise with a User model. I want to use Alchemy just to manage a few mostly-static pages like "about us" etc.

So I followed the directions for installing Alchemy CMS 3.1.0-rc1 into the existing app, and set Alchemy.user_class_name in the initializer. I mounted Alchemy under the /p/ path for now to avoid collisions with my existing paths.

Everything seems to be working fine, except when I try to view an Alchemy page while logged out, my application.html.erb throws the following error:

undefined method `new_user_session_path' for
#<#<Class:0x007fe4c6833ee0>:0x007fe4cb5b8940>

This happens because my app uses new_user_session_path in application.html.erb, to show a Login link for the guest user. In the regular app, it works fine, and also works fine when I view an Alchemy page when logged in.

I'm not familiar enough with Devise and Alchemy to figure out where the problem exists. I'm guessing it's one of two things:

  1. when there is no logged-in User, the app is creating a "guest" User (for access to other methods on the User model), and Devise doesn't know about this user so it doesn't create the new_user_session_path helper.
  2. I have some problem in my routing, and because Alchemy is a mountable engine, there is maybe some logic in my application controller that isn't getting called.

I'd prefer to not post my entire routes.rb or application controller, but here's the relevant devise section from the former.

  devise_for :users, :path => "auth", :path_names => { :sign_in => 'login', :sign_out => 'logout',
    :password => 'secret', :confirmation => 'verification', :registration => 'register' },
    :controllers => {
        :registrations => "authentication",
        :passwords => "passwords",
        :omniauth_callbacks => "omniauth_callbacks",
        :sessions => "sessions"
      }

  devise_scope :user do
    # several get/post definitions here to change various urls
  end

I don't think it's #2, because even if I define a devise_scope for a custom path like:

devise_scope :user do
  get 'login', to: 'devise/sessions#new'
end

I get the same problem: it works in the main app, and when users are logged in, but not on Alchemy pages with guest User.


Solution

  • Common rails routing proxy problem. Since the Alchemy views don't know your main apps routes, you need to use the 'main_app' routing proxy object.

    So calling 'main_app.new_user_session_path' should fix your problem.

    Read more about routes in engines in this Rails guide: http://guides.rubyonrails.org/engines.html#routes