简体   繁体   English

为什么我的Rails引擎没有安装在正确的路径下?

[英]Why doesn't my Rails engine mount under the correct path?

I have a Rails engine with these routes: 我有以下路线的Rails引擎:

Project::Engine.routes.draw do
  post '/sessions' => 'project/sessions#create'
  get '/login'  => "project/sessions#new", as: :login
  get '/logout' => "project/sessions#destroy", as: :logout

  root to: 'project/home#dashboard'
end

In my main Rails app, I have the engine's routes mounted like this: 在我的主要Rails应用程序中,我像这样安装了引擎的路线:

CMS::Application.routes.draw do
  mount Project::Engine => '/project', as: 'project'
end

I would think that this would mean that I would now have a /project/login route and /project/logout /project/sessions routes, but instead they are all under root( /sessions , /login , /logout ). 我认为这意味着我现在将拥有一个/project/login路由和/project/logout /project/sessions路由,但是它们都位于root( /sessions/login/logout )下。

If this isn't going to do what I want, then what exactly is going on here when I specify the path here? 如果这不能满足我的要求,那么当我在此处指定路径时,这到底是怎么回事? I'm using Rails 4.2.1. 我正在使用Rails 4.2.1。

I believe after testing that the problem is that you use 'project/[...]' when drawing the routes inside the engine. 我相信经过测试,问题在于您在绘制引擎内部的路线时使用了“ project / [...]”。 You mount the engine as project in the main app. 您将引擎作为项目安装在主应用程序中。 Project is out of scope when referenced inside the engine. 在引擎内部引用时,项目超出范围。

Changing to the following should make it work. 更改为以下内容应使其起作用。

Project::Engine.routes.draw do
  post '/sessions' => 'sessions#create'
  get '/login'  => "sessions#new", as: :login
  get '/logout' => "sessions#destroy", as: :logout

  root to: 'home#dashboard'
end

Then mounting the engine with: 然后通过以下方式安装发动机:

CMS::Application.routes.draw do
  mount Project::Engine => '/project', as: 'project'
end

Gives the paths: 给出路径:

  • /project /项目
  • /project/sessions /项目/会话
  • /project/login /项目/登录
  • /project/logout /项目/登出

See: 看到:

http://guides.rubyonrails.org/engines.html#mounting-the-engine http://guides.rubyonrails.org/engines.html#routes http://guides.rubyonrails.org/engines.html#mounting-the-engine http://guides.rubyonrails.org/engines.html#routes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM