ruby-on-rails - '当前页面?' 不适用于应用程序布局
问题描述
我有一个显示商店列表的 rails 应用程序。当用户在商店页面(/merchant/:username)上时,我试图让它隐藏标题。
在我的 application.html.erb
<% if current_page?(menu_path) %>
<% else %>
<%= render "shared/header" %>
<% end %>
我的路线:
match '/merchant/:username', to: 'merchants#show', via: 'get', as: 'menu'
我也试过:
<% if current_page?(:controller => 'merchants', :action => 'show') %>
这两个都在 /merchant/:username 页面中工作,并像预期的那样隐藏标题,但在主页上我收到错误:
No route matches {:action=>"show", :controller=>"merchants"}
和
No route matches {:action=>"show", :controller=>"merchants"}, missing required keys: [:username]
解决方案
您的路线需要一个参数 ( :username
),当您在主页上时该参数不存在。
你可以试试这个:
<% if controller.controller_name == 'merchants' &&
controller.action_name == 'show' %>
<% else %>
<%= render "shared/header" %>
<% end %>
编辑:添加controller_name
也许您可以content_for
在商家的观点中使用 o partials。
推荐阅读
- php - 如何将多个数组转换为单个数组
- javascript - 无法导出 Javascript 函数
- php - 将字符串回显到输入字段的值属性中仅显示部分值?
- regex - 在ruby中提取单引号之间的日期
- database - productid 未保存在供应商数据库中
- javascript - 在 React 应用程序上显示来自 woocommerce rest API 的产品变体
- python - 如何使用 KerasClassifier 使用 LSTM 进行序列分类
- bixby - 结构等价
- javascript - For 循环仅适用于带有 React 钩子的第二个循环
- scipy - sum() 不接受关键字参数,Python