1

The problem is, In the menu of my app I want to check if the current user has a book. If they do I will show a link to the edit book path, if not, I will show a link to the create book path.

<% if current_user.book? %>
<% else %>
<% end %>
Gerard
  • 95
  • 1
  • 10

1 Answers1

0

Yes, you should be able to access current_user from any controller. But always make sure you handle if current_user returns nil. You can use a try or safe navigation.

<% if current_user.try(:book?) %>
<% else %>
<% end %>

Or

<% if current_user&.book? %>
<% else %>
<% end %>
lacostenycoder
  • 10,623
  • 4
  • 31
  • 48
  • I tried your answer and it seems to only show the code thats in the ELSE section regardless of whether of not the user has a book. Any ideas? Thanks for your help. – Gerard Mar 25 '19 at 23:25
  • I can't say why unless you're sure `.book?` returns true, which defies the logic here. If you use `pry` or `bye-bug` or any such debugging tool, try to add one right before this line of code and make sure your expectations are correct. – lacostenycoder Jan 30 '20 at 00:11