0

I am trying to make sure that the following method

           def current_user 
                current_user = current_member
           end

Is available to all actions in all my controllers I have tried putting it in the ApplicationsController with no luck.

I tried using solutions in the following

Where to put Ruby helper methods for Rails controllers?

With no effect.

What is the Rails-way solution to this?

I have the same method in my ApplicationsHelper and I can access it in my views no problem.

EDIT:

To give more detail. I had an application with an authentication system I built from scratch and this used a function in a SessionHelper file called "current_user"

I have been implementing Devise into my app, and maintained my user model to hold the user details, but created a member model to hold the devise authentication information (i.e. keep the user profile info separate from the table devise is using as suggested by the doc).

This gives me a devise helper method called current_member (based on my naming of the model).

I have "current_user" all over my app, both in controller actions and in views.

I want to create an app-wide helper that will alias current_member to current_user. Strictly speaking in my question my function is wrong - this will assign current_user to an instance of the member class. Since there is a one to one relationship between member and user, with the foreign key being member.id the correct function is....

def current_user if member_signed_in? current_user = User.find_by_member_id(current_member.id) end end

My ApplicationHelper:

   module ApplicationHelper


     def current_user
       if member_signed_in?
       current_user = User.find_by_member_id(current_member.id)
      end
    end
  end

This takes care of current_user in all the views, But I can't get it to work in the controllers...see for example this code in my "show" action of the UserController

def show
    @associates = []
    @colleagues = current_user.nearbys(1000).take(20)
    @colleagues.each do |associate|
       unless current_user.following?(associate) || current_user == associate
       @associates.push(associate)
    end
   end
    impressionist(@user)
end

Forget the logic- I am just using geocoder to find nearly users. Its that current_user is resolving to "nil".

Even if I put

  before_action :current_user

    def current_user
       if member_signed_in?
       current_user = User.find_by_member_id(current_member.id)
      end
    end

In the UserController, current_user isn't working within the action. I have current_user in actions of other controllers too and the app breaks at these points, but not when current_user is in a view.

Let me know if you need more info.

EDIT 2:

I added

  before_action :authenticate_member!

To the UsersController, but this still had no effect.

EDIT 3:

I'm an idiot. The nil class error was occurring because I had no seed data in the database, thus the

      @colleagues = current_user.nearbys(1000).take(20) 

@colleagues was nil, and therefore calling "take" on nil was throwing an error. Rookie mistake.

Community
  • 1
  • 1
GhostRider
  • 2,109
  • 7
  • 35
  • 53

2 Answers2

0

When you define application actions, if you want them to be available across all other actions you need to set a before filter. So in your application controller you would have something like:

before_action :current_user

def current_user
    current_user = current_member
end

This would run this action before any other action in your application regardless of the controller

abbott567
  • 862
  • 6
  • 18
  • can you show any more of your code? It's hard to locate what the error is without seeing your code or the error... – abbott567 Apr 18 '15 at 07:48
0

I think there are two parts of your question.

1 - where to put functions that are needed across all controllers ?

So the general answer is to put them in the ApplicationController, because normally all the other controllers are inheriting from ApplicationController

2 - About the error you are getting.

My guess is , you are not loading devise before you call the devise method. So try something like

class ApplicationController < ActionController::Base
   before_action :authenticate_member!
   before_action :current_user

   def current_user
     #your method
   end
end

and as a suggestion, since you are using the same method in the helpers, to keep things DRY, you can make the controller method a helper method. So it will be available across the views

class ApplicationController < ActionController::Base
  helper_method :current_user
  before_action :authenticate_member!
  before_action :current_user

  def current_user
    #your method
  end
end

so in your view you can use current_user

If all of that fails, as @abbott567 said post your error log.

HTH

sameera207
  • 16,547
  • 19
  • 87
  • 152