RailsCasts Pro episodes are now free!

Learn more or hide this

aesacus's Profile

GitHub User: aesacus

Site: ada.evergreen.edu/~boljay13

Comments by aesacus

Avatar

respond_with is so sweet. It's odd that the generators with rails 3 still generate respond_to dos.

Avatar

Okay, this is specifically for controller tests. Add this line to your authenticated tests:

ruby
request.cookies[:auth_token] = @admin.auth_token

I spent a long time messing with posting the sessions create action, stubs, etc, but finally realized it all just comes down to a cookie.

Avatar

I am also having a lot of trouble testing this (I am using rspec). I've been working all day on it, and if I figure it out I'll post it.

Avatar

This was posted a while ago, I know, but you should look at the update action in your password_resets controller and make sure that it isn't redirecting to the index.

Avatar

According to the FactoryGirl documentation a lot of the syntax in this episode is outdated. Here is some of it rewritten following this.

ruby
FactoryGirl.define do

  sequence :username do |n| "foo#{n}" end
  sequence :email do |n| "foo#{n}@example.com" end

  factory :user, :class => User do
    username "foo"
    password "foobar"
    password_confirmation {|u| u.password}
    email "foo@example.com"
  end
end  
Avatar

Regarding my above modifications, I needed "if password.present?" in the encrypt_password expression, otherwise it will end up encrypting an empty string when you save the user. You could also do this in a before_create callback or similar so it doesn't have to run on every single save.

Also, the find_by_email and authenticate methods need to be separated out. I changed it to:

...
user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password])
...

Which I don't really like since I find relying on short circuit evaluation to be iffy.

Avatar

I forgot to add that with the above changes, you can remove the "password_salt" column.

Avatar

I have a few small simplifications that I'll share. I'm new to Rails (but not programming in general) so I apologize if anything is bad practice.

ruby
# In the User model. This is now an instance method.
def authenticate(password)
  self if Password.new(password_hash) == password
end

def encrypt_password
  self.password_hash = Password.create(password)
end
ruby
# In the users controller. 
def create
  user = User.find_by_email(params[:email]).authenticate(params[:password])
  ...etc...
end
Avatar

Maxsy and airyym,

Regarding the "uninitialized constant User::BCrypt" problem, you need to run "bundle install" in the terminal and then restart the rails server.