How and where to add a Devise view helper method current_user boolean?
This is the method I would like to access in my views:
def current_user?(user) user == current_user end
I essentially need to check that the current user can not friend and unfriend themselves in the view.
<% unless current_user?(@user) %> <% if current_user.friendly?(@user) <%= render 'unfriend' %> <% else %> <%= render 'friend' %> <% end %> <% end %>
The current_user helper method is already provided by Devise. How and where do I add this method in my Rails project?
Thanks for your help
In my opinion you could simple add the comparison on the view like this:
<% if current_user != @user %> <% if current_user.friendly?(@user) <%= render 'unfriend' %> <% else %> <%= render 'friend' %> <% end %> <% end %>
Anyway if you still want to use the method you could create a method in User.rb
def equal_user?(another_user) another_user == self end
and in the view
<% if @user.equal_user?(current_user) %>
I made a mistake initially in the application_helper.rb file.
I used an instance variable current_user?(@user) instead of the local variable current_user?(user).
If I was not using Devise I would have put the helper method in the sessions helper. So i figured it would work in the appication helper but was not positive.
To use application helper method in devise views. Place your class_eval code at the bottom of Devise initializer, Just after the Devise.setup block.
Devise.setup do |config| # ... existing Devise configuration, no modification necessary to include helper(s) ... end
Devise::Mailer.class_eval do helper :application # include "ApplicationHelper", adjust to suit your needs end