首页 > 解决方案 > '当前页面?' 不适用于应用程序布局

问题描述

我有一个显示商店列表的 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]

标签: ruby-on-railsruby

解决方案


您的路线需要一个参数 ( :username),当您在主页上时该参数不存在。

你可以试试这个:

<% if controller.controller_name == 'merchants' &&
      controller.action_name == 'show' %>
<% else %>
<%= render "shared/header" %>
<% end %>

编辑:添加controller_name

也许您可以content_for在商家的观点中使用 o partials。


推荐阅读