簡體   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