Tag: rails engines

可安装的发动机安装在哪条路径上

我需要知道,从可安装引擎的布局内部,它当前正在安装的路径。 怎么办呢? 例如我的routes.rb包含以下行: mount BackendCore::Engine => “/backend” 从BackendCore内部,我需要访问“/ backend”的值。

Rails引擎 – 导入未找到或不可读的文件:font-awesome

我做了一个超级简单的rails应用程序,使用font-awesome没有问题。 扩展此操作以在rails引擎中执行相同的步骤会产生以下错误。 File to import not found or unreadable: font-awesome 我无法找到解决方案。 如果有人有关于如何使这个简单的rails引擎与font-awesome一起工作的建议,我将非常感激。 生成rails引擎和设置font-awesome的步骤… 使用一个模型类创建基本引擎进行测试 rails plugin new testeng –full –mountable cd testeng bundle install rails g scaffold book title:string desc:string rake db:migrate 加入font-awesome 编辑testeng.gemspec并在包含rails gem之后添加sass-rails和font-awesome gems s.add_dependency ‘sass-rails’, ‘~> 4.0.3’ s.add_dependency ‘font-awesome-rails’ 将application.css重命名为application.css.scss cd app/assets/stylesheets/testeng/ mv application.css application.css.scss 编辑app / assets / stylesheets / testeng […]

在引擎中使用观察者

我创建了一个基本上用于我们所有项目的引擎。 现在我要做的是为此Engine中的所有模型添加一个before_create回调。 经过一番搜索,我发现观察者是可行的方法。 所以,我创建了这个观察者: # app/models/baco/auth/auth_observer class Baco::Auth::AuthObserver < ActiveRecord::Observer def before_create( record ) p record end end 现在我需要将它添加到应用程序中,但当然在我的引擎中没有application.rb这样的文件,所以我把它放在我的引擎中: # lib/baco/auth/engine.rb require ‘rails’ require ‘devise’ module Baco module Auth class Engine < Rails::Engine engine_name 'baco_auth' config.active_record.observers = :auth_observer end end end 但是在启动服务器时出现以下错误: …/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.1/lib/active_support/inflector/methods.rb:229:in `block in constantize’: uninitialized constant AuthObserver (NameError)

如何在.gemspec文件中将本地gem的依赖项添加到rails插件/引擎

我试过这样的方式: s.add_dependency ‘gem’, :path => ‘../gem’ 比如在gemfile,添加gem gemfile,但它不起作用,并会导致此错误: /Users/chenqh/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/requirement.rb:81:in `parse’: Illformed requirement

覆盖Rails 3 Engines提供的命名路由

我正在使用Rails引擎的Ruby on Rails 3(.0)应用程序。 但是,在我的本地应用程序中,我想覆盖Rails引擎提供的其中一个路由。 从引擎config / routes.rb: match ‘their_named_route’ => ‘controller#action’, :as => ‘the_route’ 从我的应用程序config / routes.rb: match ‘my_named_route’ => ‘controller#action’, :as => ‘the_route’ 然而,当我检查路线时,两者似乎都是活动的(并且它们的路线似乎“赢”,至少在发动机控制器内) $ rake routes the_route /my_named_route(.:format) {:controller=>”controller”, :action=>”action”} the_route /their_named_route(.:format) {:controller=>”controller”, :action=>”action”} 有没有一种方法可以强制我的本地应用程序的命名路由优先?

列出可安装的Rails 3.1引擎的“rake路由”

我正在使用可安装的引擎与Rails 3.1一起使用,我想列出引擎的路由。 我用以下方法创建了引擎: $ rails plugin new rails_blog_engine –mountable 并编辑了’test / dummy / config / routes’文件,内容如下: Rails.application.routes.draw do mount RailsBlogEngine::Engine => “/blog” end …和’config / routes’读取: RailsBlogEngine::Engine.routes.draw do resources :posts end 我想列出为’:posts’生成的路线,但目前尚不清楚我是如何做到这一点的。 当我运行’rake app:routes’时,我只得到“/ blog”路线: $ rake app:routes rails_blog_engine /blog {:to=>RailsBlogEngine::Engine} 当我运行’rake routes’时,我收到一个错误: $ rake routes rake aborted! Don’t know how to build task ‘routes’ […]

Rails 3.1:在客户端应用程序中公开引擎助手的更好方法

我找到了一些文章来解决引擎中的助手问题,这些问题不适用于消费(父)应用程序。 为了确保我们都在同一页上,让我们说我们有这个: module MyEngine module ImportantHelper def some_important_helper …do something important… end end end 如果您查看“隔离引擎的助手”(L293)中的rails引擎文档,它会说: # Sometimes you may want to isolate engine, but use helpers that are defined for it. # If you want to share just a few specific helpers you can add them to application’s # helpers in ApplicationController: # # class […]

在主应用程序中扩展Rails 3引擎的控制器

我在我的应用程序中使用Rails引擎作为gem。 引擎有PostsController ,有很多方法,我想在我的主应用程序中扩展控制器逻辑,例如添加一些方法。 如果我只是在主应用程序中创建PostsController ,则不会加载引擎的控制器。 有一个解决方案提出了Rails引擎,它基于改变ActiveSupport::Dependencies#require_or_load来扩展function 这是唯一/正确的方法吗? 如果是,我在哪里放这段代码? EDIT1: 这是Andrius为Rails 2.x 建议的代码 module ActiveSupport::Dependencies alias_method :require_or_load_without_multiple, :require_or_load def require_or_load(file_name, const_path = nil) if file_name.starts_with?(RAILS_ROOT + ‘/app’) relative_name = file_name.gsub(RAILS_ROOT, ”) @engine_paths ||= Rails::Initializer.new(Rails.configuration).plugin_loader.engines.collect {|plugin| plugin.directory } @engine_paths.each do |path| engine_file = File.join(path, relative_name) require_or_load_without_multiple(engine_file, const_path) if File.file?(engine_file) end end require_or_load_without_multiple(file_name, const_path) end end

Rails 5“gemify”资产清单文件

更新 得到这个工作状态。 gem可以在这里找到: https : //github.com/jakehockey10/popcircle 原帖: 我试图将一个jquery插件“gemify”作为一种学习体验。 我正在使用的插件也依赖于jquery.easing ,因此在gem的vendor/assets/javascripts文件夹中,我同时拥有jquery.easing.js以及其他jquery插件。 这是我的gem的.rb文件的样子: require ‘/version’ module class Engine < ::Rails::Engine class Engine < ::Rails::Engine initializer '.assets.precompile’ do |app| app.config.assets.precompile += %w( big_round.png one.png two.png three.png four.png five.png ) end end end 这是gem的当前结构: . ├── bin │ ├── console │ └── setup ├── CODE_OF_CONDUCT.md ├── Gemfile ├── Gemfile.lock […]

如何在自定义Rails引擎gem中加载正确版本的动态库和gems(libxml,Nokogiri)?

我正在构建一个使用Nokogiri gem的rails引擎,我在MacOS 10.7和10.8中遇到了我的开发设置问题: 为了让Nokogiri在我的设置中正常工作,我更新了libxml和libxslt库: brew update brew install libxslt brew upgrade libxml2 gem uninstall nokogiri gem install nokogiri — –with-xml2-include=/usr/local/Cellar/libxml2/2.9.0/include/libxml2 –with-xml2-lib=/usr/local/Cellar/libxml2/2.9.0/lib –with-xslt-dir=/usr/local/Cellar/libxslt/1.1.28 如果我在“常规”rails应用程序中使用它,Nokogiri可以正常工作(将它包含在Gemfile中,运行bundle install)。 当我尝试从我正在构建并使用我的应用程序作为gem的Rails引擎中使用Nokogiri时出现问题。 在gemspec中: s.add_dependency ‘nokogiri’ # XML parsing 我还在Gem的Gemfile顶部尝试了以下内容: gem ‘nokogiri’ 当我启动一个使用我的Rails Engine gem的应用程序时,我收到以下警告: WARNING: Nokogiri was built against LibXML version 2.9.0, but has dynamically loaded 2.7.8 如何配置gem和/或bundler和/或Rails以使用正确版本的Nokogiri和libxml以及libxslt? 另外,这是nokogiri -v的输出 # Nokogiri […]