如何从主应用程序访问Rails Engines方法? [英] How to access Rails Engines methods from main application?

查看:89
本文介绍了如何从主应用程序访问Rails Engines方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Spree :: Core引擎中定义的current_order方法: https://github.com/spree/spree/blob/master/core/lib/spree/core/current_order.rb

我认为我已经尝试过

Spree::Core::CurrentOrder.current_order

虽然在开发中仅使用"current_order"是可行的,但在生产中则不能.

因此,我试图像这样在我的视图文件中要求它:

require 'spree/core/current_order'

我还尝试了其他解决方案的排列:

如何合并Rails Engine ApplicationController方法在主应用程序中?

从引擎向应用程序

轨道3.1:在客户端应用中公开引擎帮助器的更好方法

但是我没有真正去做.

有人能指出我正确的方向吗?也许我在上述链接中错误地实施了解决方案?

这是我在生产中遇到的错误:

2012-06-21T09:59:08 + 00:00 app [web.1]:ActionView :: Template :: Error(针对Spree :: Core :: CurrentOrder:Module的未定义方法"current_order"):

如果我用current_order注释掉代码行,那么一切都会在生产环境中起作用.

我在想这是产品在生产中的装载方式?但这是我第一次尝试部署,因此我不太了解开发和生产之间的区别.

提前谢谢!

解决方案

这个问题有点老了,但这里有一个可能的答案,记录下来.

也许您可以通过在控制器中使用一段狂欢代码来解决此问题.通常,最好将代码包含在控制器中(或者对于Spree,在 controller_decorator ),而不是视图中.

例如,如果您在产品视图中需要使用@current_order:

Spree::ProductsController.class_eval do
  @current_order = Spree::Order.find(session[:order_id])
end

但是我不知道为什么它在开发中而不是在生产中起作用.

I'm trying to use the current_order method defined in the Spree::Core engine: https://github.com/spree/spree/blob/master/core/lib/spree/core/current_order.rb

In my view, I've tried

Spree::Core::CurrentOrder.current_order

Using just "current_order" in development works fine though, but not in production.

So then I've tried to require it in my views file like this:

require 'spree/core/current_order'

I've also tried permutations of these other solutions:

How to incorporate Rails Engine ApplicationController methods in a main app?

A way to add before_filter from engine to application

Rails 3.1: Better way to expose an engine's helper within the client app

But I've lost track of what I actually did.

Can someone please point me in the right direction? Maybe I implemented the solutions in the above links incorrectly?

This is the error I'm getting in production:

2012-06-21T09:59:08+00:00 app[web.1]: ActionView::Template::Error (undefined method `current_order' for Spree::Core::CurrentOrder:Module):

If I comment out the lines of code with current_order, everything works in production.

I'm thinking it's the way things are loaded in production? But this is the first time I'm trying to deploy so I don't quite understand the differences between development and production.

Thanks in advance!

解决方案

This question is a bit old, but here is a possible answer, for the record.

Maybe you can work around by using a piece of the spree code in your controller. It's generally a better idea to have the code in the controller (or, in the case of Spree, in a controller_decorator), than in the view.

For example if you need this in your products view as @current_order:

Spree::ProductsController.class_eval do
  @current_order = Spree::Order.find(session[:order_id])
end

But I don't know why it's working in development and not in production.

这篇关于如何从主应用程序访问Rails Engines方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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