在视图中调用命名路由助手以 NameError 结束

Invoking named routes helpers in views ends with NameError

在视图中调用命名路由助手以以下配置的 NameError 结尾:

routes.rb:

scope :orders, as: :orders do
  scope '/:order_id', as: :order do
    post :returns, :to => 'order_returns#create'
  end
end

$rake routes:

orders_order_returns POST /orders/:order_id/returns(.:format) order_returns#create

当我将 <%= orders_order_returns_path %> 添加到模板时,Rails退出 undefined local variable or method 'orders_order_returns_path' for #<#<Class:0x007faec10fe728>:0x007faec10dc8d0>...

正在异常页面 returns 的控制台中执行 Rails.application.routes.named_routes.helpers.map(&:to_s):

["spree_path", "orders_order_returns_path", "rails_info_properties_path", "rails_info_routes_path", "rails_info_path", "rails_mailers_path", "spree_url", "orders_order_returns_url", "rails_info_properties_url", "rails_info_routes_url", "rails_info_url", "rails_mailers_url"]

我的问题是:为什么在视图中使用命名路由 path/url 助手以异常结束,即使它们在控制台中可见?

我找到了答案 here:

Because you're calling this inside a spree template you need to prefix it with main_app., like main_app.products_starting_with_path

这是 Spree 的发行说明:

Conversely, to reference routes from the main application within a controller, view or template from Spree, you must use the main_app routing proxy

使用main_app路由代理解决了描述的问题:)