Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create a current_user method without using devise

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 :)


1 Answers

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
like image 54
RafalG. Avatar answered Mar 19 '26 08:03

RafalG.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!