以下配置,在视图中调用命名路由帮助程序以 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>
...
在控制台中对异常页面执行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"]
我的问题是:为什么在视图中使用命名路由路径/url 帮助程序结束,即使它们在控制台中可见?
我在这里找到了答案:
因为您在 spree 模板中调用它,所以您需要在它前面加上
main_app.
,就像main_app.products_starting_with_path
以下是 Spree 的发行说明:
相反,要引用来自主应用程序中的路由 控制器、视图或模板 从 Spree 中,您必须使用
main_app
路由代理
使用main_app
路由代理解决了所描述的问题:)