Reputation:
I want to create a Current_User method but I don't want to use a gem or similar. How would I do that in Rails 4.1.2
Questions_Controller where i want the current_user method.
class QuestionsController < ApplicationController
before_filter :auth, only: [:create, :your_questions, :edit, :update]
# def index
# @question = Question.new
# @questions = Question.unsolved(params)
# end
@questions = current_user.your_questions(params[:page])
def your_questions(page)
questions.paginate(page: page, order: 'created_at DESC', per_page: 3)
end
def self.unsolved(params)
order('created_at DESC').where(solved: false).paginate(page: params[:page],per_page: 3)
end
def create
@question = current_user.questions.build(params[:question])
if @question.save
flash[:success] = 'Your question has been posted!'
redirect_to @question
else
@questions = Question.unsolved(params)
render 'index'
end
end
def new
@question = Question.new
end
def show
# raise FOO
puts params
@question = Question.find(params[:id])
@answer = Answer.new
end
def your_questions
@questions = current_user.your_questions(params[:page])
# current_user.your_questions(params[:id])
end
def edit
@question = current_user.questions.find(params[:id])
end
def update
@question = current_user.questions.find(params[:id])
if @question.update_attributes(params[:question])
flash[:success] = 'Your question has been updated!'
redirect_to @question
else
render 'edit'
end
end
def search
@questions = Question.search(params)
end
end
My user model
class User < ActiveRecord::Base
has_many :questions
has_many :answers
# attr_accessible :username, :password, :password_confirmation
has_secure_password
# validates :username, presence: true, uniqueness: { case_sensitive: false },
# length: { in: 4..12 },
# format: { with: /A[a-z][a-z0-9]*z/, message: 'can only contain lowercase letters and numbers' }
validates :password, length: { in: 4..8 }
validates :password_confirmation, length: { in: 4..8 }
def your_questions(page)
questions.paginate(page: page, order: 'created_at DESC', per_page: 3)
end
end
My application controller
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
# protect_from_forgery
helper_method [:current_user, :logged_in?]
protected
private
def login(user)
session[:user_id] = user.id
end
def current_user
current_user ||= User.find(session[:user_id]) if session[:user_id]
end
def logged_in?
!current_user.nil?
end
def auth
redirect_to login_url, alert: 'You must login to access that page' unless logged_in?
end
end
If there are more files you want me to add to the question please comment im a novice ruby on rails developer :)
Upvotes: 2
Views: 5229
Reputation: 29328
I think your problem is not in the controller but in you helpers.
It is calling methods which are looking for a local variable current_user
and there isn't one. You need to instantize these like this:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
helper_method [:current_user, :logged_in?]
private
def login(user)
session[:user_id] = user.id
end
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
def logged_in?
!current_user.nil?
end
def auth
redirect_to login_url, alert: 'You must login to access that page' unless logged_in?
end
end
Like @RafalG. stated see the @
in front of current_user
. This will create an instance variable to track around instead of referencing a local variable that is missing.
Also note your current current_user
method will always run the User.find
because the local variable current_user
will always be nil
inside this scope, thus why you need to make it part of the instance.
UPDATE
I will leave the above for edification because you should still create an instance. I think this is the real issue
class QuestionsController < ApplicationController
before_filter :auth, only: [:create, :your_questions, :edit, :update]
# def index
# @question = Question.new
# @questions = Question.unsolved(params)
# end
#vvvv This Line is out of a scope and will raise errors vvv#
@questions = current_user.your_questions(params[:page])
def your_questions(page)
questions.paginate(page: page, order: 'created_at DESC', per_page: 3)
end
....
end
If you want to do this you would declare it in a before_filter
call back because right now Rails
has no idea how to handle this statement appropriately and outside of a method it will not have access to any of your helpers.
Upvotes: 2
Reputation: 81
class ApplicationController < ActionController::Base
helper_method :current_user
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
end
Upvotes: 8