繁体   English   中英

Rails,current_page?(user_path)给出错误

[英]Rails, current_page?(user_path) giving an error

我有我想要隐藏在特定页面上的元素巫婆,例如位于app/views/users/ (我有new.html.erbedit.html.erbshow.html.erb 。我还有div in我的layouts/application.html.erb它将显示在所有页面上,因此我想将其隐藏。

我以为我可以这样做:

<% unless current_page?(new_user_path) || current_page?(user_path) %>
  <div>Some content</div>
<% end %>

但这会给我一个错误,非常明显:对于user_show方法,他需要用户的ID,但是我们不会访问存在@user变量的页面。 您能帮我一下吗:

  1. 有可能解决此错误吗? (而且我不想在每个位置分配@user变量,也不想在所有页面上都列出允许的内容)

  2. 还有其他方法可以隐藏特定页面上的元素吗?

不能完全确定您要实现的目标,但是这可以防止用户不在场:

unless current_page?(new_user_path) || @user && current_page?(user_path(@user))

我认为您可能想要的是:

<% unless current_page?(controller: 'users') %>
  <div>Some content</div>
<% end %>

通过传递controller: 'users'您可以捕获该控制器的所有路由(动作),包括新的/编辑/显示的路由。

有关更多详细信息,请参阅文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM