Reputation: 15
This is my first rails project. I have a web app with both a 'subscriber' and 'user' controller and module. (subscribers only gave their email, users are signed in)
I'm trying to get users access to the list of subscribers, by creating an 'index' method in the subscriber controller, accessible only to users who are signed in.
I'm trying to do it using this
class SubscribersController < ApplicationController
before_filter :signed_in_user, only: [:index]
def index
@subscribers = Subscriber.all
end
Where signed_in_user is defined as follows:
class UsersController < ApplicationController
def signed_in_user
unless signed_in?
store_location
redirect_to signin_url, notice: "Please sign in."
end
end
and signed_in? is in the sessions helper:
module SessionsHelper def sign_in(user) cookies.permanent[:remember_token] = user.remember_token self.current_user = user end
def current_user=(user)
@current_user = user
end
def current_user
@current_user ||= User.find_by_remember_token(cookies[:remember_token])
end
def signed_in?
!current_user.nil?
end
But it doesn't work as I don't have access to signed_in_user from the subscribers controller. What would be the 'right' way to go about doing this?
Thanks!
Upvotes: 1
Views: 4098
Reputation: 159
In your UsersController include SessionsHelper, so that the function is accessible.
class UsersController < ApplicationController
include SessionsHelper
.......
end
Functions like signed_in? which are generally needed in many controllers, better to include helper in the application controller itself.
Upvotes: 2