直接从URL路径访问rails引擎路由

时间:2018-03-23 09:38:14

标签: ruby-on-rails ruby devise rails-engines

我正在尝试访问

POST http://localhost:3000/login

MyEngine路线

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

我的申请路线

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

评论隔离空间

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

点击POST http://localhost:3000/login

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

如何直接从URL路径访问Engine的登录方法?

1 个答案:

答案 0 :(得分:1)

错误未初始化的常量SessionsController 正在发生,因为rails无法找到您的会话控制器,因此如果您覆盖默认设置,请指定路径内的自定义控制器路径。

  

引擎路线

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"}在这里你可以看到我已经指定了会话路径。

  

config / routes.rb

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

您的会话控制器如下:

  MyEngine::SessionsController < Devise::SessionsController
  end