Ashwini
Ashwini

Reputation: 2497

Access rails engine routes directly from URL path

I am trying to access

POST http://localhost:3000/login

MyEngine routes

devise_for :accounts, { class_name: 'MyEngine::Account', skip: [:sessions, :registration, :password] }
    as :account do
     post 'login', to: 'sessions#create'
    end

My Application Routes

Rails.application.routes.draw do
  mount MyEngine::Engine => '/engine', as: 'engine'
end

Commented isolation Space

module MyEngine
  class Engine < ::Rails::Engine
    #isolate_namespace MyEngine
  end
end

Error after hitting POST http://localhost:3000/login

#<ActionController::RoutingError: No route matches [POST] \"/login\">

How to access login method of Engine from URL path directly?

Upvotes: 0

Views: 379

Answers (1)

Vishal Taj PM
Vishal Taj PM

Reputation: 1359

The error uninitialized constant SessionsController happening because rails not able to locate your sessions controller so if you are overriding default devise specify custom controller path inside routes.

Engine routes

devise_for :accounts, { class_name: 'MyEngine::Account', skip: [:sessions, :registration, :password], controllers: { sessions: "my_engine/sessions"} } as :account do
 post 'login', to: 'sessions#create'
end

controllers: { sessions: "my_engine/sessions"} here you can see i have specify the sessions path.

config/routes.rb

Rails.application.routes.draw do
  # your main routes 
  mount MyEngine::Engine => '/engine', as: 'engine'
end

your sessions controller will be as follows:

  MyEngine::SessionsController < Devise::SessionsController
  end

Upvotes: 1

Related Questions