我想隐藏在特定页面上的元素女巫,例如位于app/views/users/
的页面(我有new.html.erb
; edit.html.erb
; show.html.erb
。我有我的layouts/application.html.erb
中的div会显示在所有页面上,所以我想隐藏它。
我以为我可以这样做:
<% unless current_page?(new_user_path) || current_page?(user_path) %>
<div>Some content</div>
<% end %>
但它会给我一个错误,很明显:对于user_show方法,他需要用户的id,但我们不会访问变量@user
存在的页面。你可以给我一个帮助:
是否有可能解决此错误? (而且我不想在每个地方分配@user变量,而且我不想让所有页面的列表都被允许)
有没有其他方法可以隐藏特定页面上的元素?
答案 0 :(得分:2)
不完全确定您要实现的目标,但这样可以防止用户不在场:
unless current_page?(new_user_path) || @user && current_page?(user_path(@user))
答案 1 :(得分:1)
我认为你可能想要的是:
<% unless current_page?(controller: 'users') %>
<div>Some content</div>
<% end %>
通过传递controller: 'users'
,您可以捕获该控制器的所有路径(操作),包括新的/编辑/显示路径。
有关详细信息,请参阅docs。