Rails 引擎渲染视图 [英] Rails Engines rendered View

查看:51
本文介绍了Rails 引擎渲染视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 Rails 引擎.这是我的第一个,除了 rails 文档中的例子,所以我没有很好的参考资料.它有一个部分,我需要应用程序呈现.

I have a rails engine I'm developing. This is my first one, besides the example in rails documentation, so I don't have a good ref on what works. It has a partial that i'm requiring the Application to render.

<%=render partial: 'my_engine/foo/bar'%>

效果很好.我明白为什么我需要通过引擎名称空间引用部分.

That works fine. And i understand why i need to reference the partial through the engine name space.

但是在部分中,我显然也必须使用名称空间.所以当我渲染照片时

But within the partial, i apparently have to use the name space as well. So when i render a photo

 <%= image_tag('my_engine/addphoto.jpg') %>

根据文档,这是反直觉的.是我做错了什么,还是就是这样?

that is counter intuitive based on the documentation. Am I doing something wrong, or is just the way it is?

好的,感谢 Rich Peck,我想我理解得更好了.让我列举一下,如果我仍然感到困惑,你可以纠正我.

Okay thanks to Rich Peck I think I understand better. Let me enumerate so you can correct me if I'm still confused.

我对如何为引擎生成命名空间感到困惑.我假设该结构生成了名称空间,并且当您在引擎中时,您会像在普通应用程序中一样操作.命名空间是自动的:).

I was confused as to how the Namespace is generated for the engine. I was assuming that the structure generated the name space, and that when you were inside the engine, you would operate just as if you were in a normal app. And the namespace was automagic:).

但如果我理解正确,rails 组件的命名空间是通过将它们封装在由生成器处理的 :::: 模块 EngineName 中生成的,但是如果您手动创建文件,那么您需要在文件中添加封装.由于这是我的第一个引擎,我不知道它们不见了.

But if I understand correctly the name space for the rails component is generated by encapsulating them in the :::: module EngineName which is taken care of by the generators, but if you're creating the file manually, then you need to add the encapsulation in the files. Since this was my first engine I didn't know that they were missing.

对于资产管道,命名空间由结构 image/enginename/.... 生成

For the Asset pipeline the namespace is generated by the structure image/enginename/....

因此,即使在引擎内,除非您在模块内,否则您必须遵守命名空间 EngineName::ModelName::Method.

So even within the engine unless your inside the module, you have to honor the namespace EngineName::ModelName::Method.

命名空间不应用于任务文件,似乎不喜欢被封装在模块中.但这无论如何都是多余的,因为 rake 任务已经提供了一个命名空间方法.

The namespace isn't applied to the task file, doesn't seem to like being incapsulated in a module. But that is redundant anyway since the rake task already provide a namespace method.

Ala  namespace :db do

我还惊讶地发现您需要手动将命名空间应用程序控制器包含在命名空间控制器中 ala

I was also surprised to find that you need to manually include the namespaced application controller in the namespaced controllers ala

require_dependency "enginename/application_controller"

但正如 Rich 所说,引擎只是一个美化的模块,所以我想它不时需要一点帮助.

but as Rich says, the engine is just a glorified module, so I guess it needs a little help now and then.

当然,Javascript 文件是隔离的,因为它们仅在加载引擎中的一个页面时加载,而不是在呈现引擎的部分页面时加载.你可以让引擎的用户在他们的 application.js 中包含 javascript 文件,但是他们需要手动命名空间以避免与来自其他来源的类似 JS 混淆.Kenneth Truyers 有一篇关于实际操作的好博客,我会书签 用于将来的练习,也许我现在需要开始.

And of course the Javascript files are isolated because they're only loaded when one of the pages from the engine are loaded, and NOT when a partial from the engine is rendered. You can have the user of the engine include the javascript file in their application.js, but then they need to be namespaced manually to avoid confusing them with similar JS from other sources. There's a good blog on actually doing that by Kenneth Truyers that I'd bookmarked for a future exercise, maybe I'll need to get to it now.

所以语言环境没有命名空间,所以你需要自己做.简单地将 :engine_name 添加到语言环境文件层次结构阿拉德::引擎名称:变量我建议为引擎名称添加一个前缀,例如 x ,这样您就可以避免使用与变量相同的名称的用户破坏您的引擎区域设置.所以 xAdmin: 会比 Admin: 更好,因为很可能像我这样的人会使用 Admin: 作为管理功能的变量:) 并发现引擎中的所有本地化突然消失了.

lso the locales are not name spaced, so you need to do that yourself. Simple to add :engine_name to the locale file hierarchy ala de: :engine_name :variables I would recommend adding a prefix to the engine name like x so that you avoid getting your engine locales clobbered by a user using the same name as a variable. So xAdmin: would be better then Admin: since it's likely someone like me would use Admin: as a variable for an admin function:) and discover that all the localization in your engines suddenly disappeared.

有钱,我更亲近了

现在扩展我的问题,

我不确定初始化文件是否有命名空间.应用程序初始化器目录中的 enginename.rb 文件是否会覆盖引擎初始化器目录中的同名文件?我假设如果这是真的,我们可以在引擎初始值设定项中使用不同的名称,因为我认为它与文件名没有真正的关联,只是簿记.

I'm not sure that the initialization files are namespaced. Does a enginename.rb file in the apps initializers directory overwrite one with the same name in the engine's initializer directory? I assume if it's true we can just use a different name in the engine initializer since I think it there are no real associations to the file name, just bookkeeping.

现在如何在应用中扩展模型.

Now how to extend a model in the app.

因此向应用程序模型添加列是否可以执行此操作在engine.rb(位于lib)
mattr_accessor :user_classmattr_accessor :user_table

So to add columns to a apps model is it okay to do this in engine.rb (located in lib)
mattr_accessor :user_class mattr_accessor :user_table

在 engine_initializer.rb 中

in the engine_initializer.rb

    engine.user_class= 'User'
    engine.user_table='users'

然后在迁移中

add_column  Engine.user_table.to_sym, :attribute   
 add_index   Engine.user_table.to_sym, :attribute,   
        name: "index_#{Engine.user_table}_on_engine_attribute".to_sym

这似乎工作正常

但是我们如何向模型添加方法.我让用户添加 require 'engine/user_include.rb'

But how do we add methods to the model. I'm having the user add require 'engine/user_include.rb'

有什么好的办法吗

推荐答案

Modules &课程

是我做错了什么,还是就是这样?

Am I doing something wrong, or is just the way it is?

你没有做错,但它不是它的方式"...让我解释它是如何工作的

You're not doing it wrong, but it's not "the way it is"... let me explain how it works

Rails 引擎只不过是美化的 modules &.创建引擎(gem)时,本质上就像创建一个命名空间控制器 -'

Rails engines are nothing more than glorified modules & Classes. When you create an engine (gem), it's essentially like creating a namespaced controller -'

#app/controllers/namespace/controller.rb
Class Namespace::Controller < ApplicationController
  ...
end

你必须记住Ruby on Rails 只是一堆模块 &.显然它被创建得非常 - 但是你必须意识到你在 Rails 中所做的一切都以某种方式涉及到某些类或模块

You have to remember Ruby on Rails is just a bunch of modules & classes. Obviously it's been created very well - but you have to realize that everything you do in Rails is involved with certain classes or modules in some way

而且由于 engines 只是一种管理这些 modules &classes,你会明白Engine 只是增加更多类的可扩展性的一种方式.

And since engines are just a way to manage these modules & classes, you'll appreciate that an Engine is just a way to add extensibility with more of them.

引擎

module Blorgh
  class Engine < Rails::Engine
    ...
  end
end

引擎 旨在让您能够在不干扰系统的情况下向系统添加标准化功能您的代码库(基本上是 gems 的工作方式).您的问题是您是否需要在路径等中调用引擎名称:

Engines are designed to give you the ability to add standardized functionality to your system without intruding on your codebase (basically how gems work). Your question is whether you need to call your engine's name in paths etc:

<%= image_tag('my_engine/addphoto.jpg') %>

最重要的是如果您将资源与引擎捆绑在一起,Rails 还如何访问这些文件?将基于引擎的文件与其他应用程序分开意味着您将能够专门为引擎捆绑文件(它们不会发生冲突).

The bottom line is if you are bundling assets with your engine, how else is Rails going to access the files? Keeping the engine-based files separate from your other application means you will be able to bundle files specifically for the engine (they won't conflict).

这就是 引擎 存在的原因 - 它们允许您提供功能而不会干扰应用程序的其他部分

This is the reason why the engines exist - they allow you to provide functionality without intruding on other parts of the application

宝石

最近在一个 gem 上工作,我可以证明引擎的方式

Having recently worked on a gem, I can attest to the way of Engines

rails gem 基本上只是一个附加了一些其他文件的引擎.因此,如果您在 gem 中包含各种 controllersmodelsassets - 让它们对 <代码>引擎(为了不与应用程序的其他部分冲突)?

A rails gem is basically just an engine with some other files attached. So if you're including various controllers, models or assets with your gem - doesn't it make sense to make them unique to the engine (as to not conflict with the other parts of the app)?

这篇关于Rails 引擎渲染视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆