在rails引擎视图中缺少设计路由助手

Jef*_*eff 9 ruby-on-rails rails-engines devise

我正在构建一个名为Engrave的Rails引擎.

我的发动机安装如下:

# Routes.rb of the host app
mount Engrave::Engine => "/engrave", :as => "engrave_engine"
Run Code Online (Sandbox Code Playgroud)

在这个引擎中,我有一个名为"PostsController"的控制器.当我导航到这个控制器来查看这样的帖子时:/engrave/posts/1我收到此错误:

undefined local variable or method `new_user_session_path'
Run Code Online (Sandbox Code Playgroud)

引擎中的PostsController继承自引擎控制器,它继承自应用程序控制器,如下所示:

module Engrave
  class PostsController < ApplicationController
  ...
end

class Engrave::ApplicationController < ApplicationController
end
Run Code Online (Sandbox Code Playgroud)

new_user_session_path由devise定义,我设置如下:

devise_for :users
Run Code Online (Sandbox Code Playgroud)

对new_user_session_path的调用layouts/application.html.erb位于主机应用程序的模板文件中

我无法弄清楚为什么这个路由助手在这种情况下不可用.我究竟做错了什么?

Str*_*ays 9

使用

main_app.new_user_session_path

应该工作


小智 6

我已成功在主应用程序的application_helper.rb中执行以下操作:

module ApplicationHelper
  # Can search for named routes directly in the main app, omitting
  # the "main_app." prefix
  def method_missing method, *args, &block
    if main_app_url_helper?(method)
      main_app.send(method, *args)
    else
      super
    end
  end

  def respond_to?(method)
    main_app_url_helper?(method) or super
  end

 private

  def main_app_url_helper?(method)
    (method.to_s.end_with?('_path') or method.to_s.end_with?('_url')) and
      main_app.respond_to?(method)
  end
end
Run Code Online (Sandbox Code Playgroud)

我在可安装的引擎中使用过它,所以你不必牺牲这些功能.