【发布时间】:2016-03-22 22:46:54
【问题描述】:
我的标题代码越来越重复,我决定将其放入引擎中并将其加载到我的所有应用程序中。
我在加载帮助程序时遇到了麻烦(即使在 engine.rb 中解决了)。
主机应用程序说 nomethod 错误,我不认为帮助文件正在加载。我也可以很好地从引擎渲染视图,但仍然无法加载帮助程序,我缺少什么吗?
代码块导致主应用出现问题。
<h1><%= yield(:phc_title) %></h1>
<span><%= yield(:phc_title_tagline) %></span>
<ol class="breadcrumb">
<li><a href="#">Home</a></li>
</ol>
/lib/phctitler/engine.rb
module Phctitler
class Engine < ::Rails::Engine
# Required Dependencies
require 'figaro'
# Isolate Namespace for PHC Members
isolate_namespace Phctitler
# Testing Generator
config.generators do |g|
g.test_framework :rspec,
fixtures: true,
view_specs: false,
helper_specs: false,
routing_specs: false,
controller_specs: true,
request_specs: false
g.fixture_replacement :factory_girl, dir: "spec/factories"
end
# Load Helper Files
config.to_prepare do
ApplicationController.helper(Phctitler::ApplicationHelper)
end
# Auto Mount Plugin
initializer "phctitler", before: :load_config_initializers do |app|
Rails.application.routes.append do
mount Phctitler::Engine, at: "/"
end
end
end
end
helpers/phctitler/application_helper.rb
module Phctitler
module ApplicationHelper
# Helper for Page Title
def phc_title(phc_page_title)
content_for :phc_title, phc_page_title.to_s
end
# Helper for Page Title Tag
def phc_title_tagline(phc_page_title_tagline)
content_for :phc_title_tagline, phc_page_title_tagline.to_s
end
end
end
【问题讨论】:
-
为一个模块使用一个成熟的引擎是不是有点矫枉过正?
-
是的,有一点,我更容易通过这种方式在许多应用程序中推出更新。我还将考虑为其添加关键字和 seo 描述助手。
标签: ruby-on-rails ruby ruby-on-rails-4