How to access Rails Engines methods from main application?

谁都会走 提交于 2019-12-04 04:00:53

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/11135876/how-to-access-rails-engines-methods-from-main-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!