Andrey Drozdov Andrey Drozdov - 2 months ago 11
Ruby Question

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?


Answer

Not entirely sure what you are trying to achieve, but this is how you can guard from user not being present:

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