Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7779071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:33:28+00:00 2026-06-01T18:33:28+00:00

I am trying to refactor the railstutorial authorization mechanism. My version of rails is

  • 0

I am trying to refactor the railstutorial authorization mechanism.

My version of rails is 3.2.0 and I am using ruby 1.9.3-p0 and postrgresql 9.1.

So far, my tests are passing when it comes to a failed attempt to sigin but the successfull sign in fails.(The reason is that I have to refactor the old signin mechanism)

Here is my session helpers sign_in function:

def sign_in(employee)
  cookies.permanent.signed[:remember_token] = [employee.id, employee.salt]
  self.current_employee = employee
end.

One problem I see immediately with the sign_in function is that has_secure_password already takes care of the encryption and salt etc … my thinking was that maybe I should use password_digest instead of employee.salt, but that failed as well.

I would like to have my cookies expire after two hours. I found this option at api.rubyonrails.org under cookies.

 cookies[:key] = {
                  value => "employee.id, employee.salt",
                  expires => 2.hours.from.now
                  }

Another question I have has to do with the fact that has_secure_password already has an authenticate method so that means that I do not have to use the authenticate definition defined in the employee model,(user model) in the rails tutorial, but when I comment it out I get a flag reading:

NoMethodError: undefined method 'authenticate'

Here is my session controllers create action:

def create
  employee = Employee.authenticate(params[:session][:email],
                                   params[:session][:password])
  if employee.nil?
    flash.now[:error] = "Invalid email/password combination."
    @title = "Sign in"
    render 'new'
   else
    sign_in employee
    redirect_back_or employee
   end
 end

It seems the Employee.authenticate is a problem.

So I essentially have three question and they are as follows:

In the rails tutorial we go through a fairly lengthly process of encrypting and applying salt etc to the employees password. Since has_secure_password has this already taken care of, what variable would I pass to my functions or arguments that would capture the encrypted password?

The next question has to do with the expiration of the cookie, and how I would use that in the sign_in function?

Lastly, how do I use the authenticate method so that rails recognizes it as a genuine method?

Just for the record, I have searched through railsguide, api.rubyonrails.org and other questions asked on SO that are similar to this one. Of course this merely points up my lack of understanding of the principles, but I am learning and do take direction well.

Thanks for any thoughts, suggestions and or resources you might share with me.

Update
I re-read the api on has_secure_password and authenticate takes only one argument, namely an unencrypted password … so I have something to work with.

I still need any help or thoughts or suggestions that you might offer … thanks.

update
I found this article that deals with session timeouts:
http://madkingsmusings.blogspot.com/2011/05/session-timeouts-on-rails.html
I am still working to see if I can get it to work for me, but it is tailored for the railstutorial.
As for the other questions, Michael Hartl is busy pushing out the second edition of Ruby on Rails tutorial and in that edition he will be dealing with has_secure_password.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:33:29+00:00Added an answer on June 1, 2026 at 6:33 pm

    The new version of the railstutorial is available.

    For the first and last question question… You’ll find the authentication extremely simple.
    In the User model:

    has_secure_password
    

    In the SessionController:

      def create
        user = User.find_by_email(params[:session][:email])
        if user && user.authenticate(params[:session][:password])
          sign_in user
          redirect_back_or user
        else
          flash.now[:error] = 'Invalid email/password combination'
          render 'new'
        end
      end
    

    In the SessionsHelper:

    def sign_in(user)
      cookies[:remember_token] = user.remember_token
      current_user = user
    end
    

    It should have been obvious but I didn’t even think about looking for the code on github. Maybe someone else will appreciate the link.
    Here is Hartl’s 2nd edition Sample_App source on github

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to refactor an old web service project using new structure (with Maven)
I am trying to refactor my package using Eclipse org.sheehan.activity to org.sheehan.stocks.activity For some
I'm trying to refactor some rather horrible code at the moment. It's passing around
I'm trying to refactor a component that currently produces a Seq[X] using a fairly
I'm trying to refactor some python code which I'm using for financial analytics processing
I'm trying to refactor some slow running code which writes XML using nested loops
I'm using RabbitMQ and trying to refactor my current native java implementation to using
trying to refactor code to provide clean association A GAME has a HOME_TEAM and
I am trying to refactor out my application a little bit to keep it
I am trying to refactor some delphi 7 code. One of the procedures I'd

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.