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

Answers


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.

Thanks @JesseWolgamott


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.

config/initializers/devise.rb

Devise.setup do |config|
  # ... existing Devise configuration, no modification necessary to include helper(s) ...
end

EDIT:

Devise::Mailer.class_eval do
  helper :application # include "ApplicationHelper", adjust to suit your needs
end

Need Your Help

Magento - New product attributes created by backend not visible

magento-1.4 magento-1.5 magento magento-1.7

i've created trough backend two new product's attributes, both locally and on test server. while locally i' able to see new attributes in product details, on test server i dont see any on the two new

Creating border around ImageIcon on a JLabel, not around the Jlabel

java swing border jlabel

I have problem with creating border for an icon in JLabel. I have JPanel in which I set it into GridLayout. I added Jlabel on the JPanel. The size of the JLabel were according to the size of the ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.