简体   繁体   中英

Rails, current_page?(user_path) giving an error

I have element witch I want hide on specific pages, for example on pages located at app/views/users/ (there I have new.html.erb ; edit.html.erb ; show.html.erb . And I have div in my layouts/application.html.erb it will be shown on all pages, so I want to hide it.

I thought i can do it like this:

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

But it will give me an error, pretty obvious: for user_show method he need an id of the user, but we are not visiting pages where variable @user is present. Can you land me a help:

  1. Any possibility to get around this error? (And I don't want to assign @user variable every where and I don't want make list of all page what are allowed)

  2. Is there any other way to hide element on specific pages?

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

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

I think what you probably want is:

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

By passing controller: 'users' you catch all routes (actions) for that controller, including the new/edit/show routes.

For more detail see the docs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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