当前页面?在嵌套资源上

时间:2019-07-02 11:04:51

标签: ruby-on-rails ruby-on-rails-5

如何使用current_page?检测嵌套资源?

让我澄清一下:我有两个控制器:ManufacturersModels

#routes.rb

resources :manufacturers do
  resources :models, except: :index
end

如果我尝试检测制造商的显示页面,那么我要做的就是:

<% if current_page?(controller: 'manufacturers', action: 'show') %>

一切正常。但是,如果我对Models控制器尝试相同的操作:

<% if current_page?(controller: 'models', action: 'show') %>

我收到此错误:No route matches {:action=>"show", :controller=>"models"}


我最终做了解决方法

<% if controller.controller_name == "models" && controller.action_name == "show" %>

但是我想知道如何使用current_page?帮助器来达到相同的结果。

希望有人可以帮助我。谢谢。

2 个答案:

答案 0 :(得分:0)

当你有这样的嵌套资源时:

path('mods/mix', MixCUD.as_view([POST])),
path('mods/mix/update/<int:mixId>', MixCUD.as_view([UPDATE])),
path('mods/mix/delete/<int:mixId>', MixCUD.as_view([DELETE])),

您的模型路径如下所示:

resources :manufacturers do resources :models, except: :index end

因此在此调用中缺少 manufacturers/:manufacturer_id/models

manufacturer_id

我通过自定义助手解决了这个问题: current_page?(controller: 'models', action: 'show')

app/helpers/application_helper.rb

答案 1 :(得分:-1)

尝试一下:

current_page?(controller: 'manufacturer/models', action: 'show')
相关问题