Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Signup form

  • Loading branch information...
commit c3d1c6aadb73e5cfcfdfc549e31e1cae7bd1eed7 1 parent c0aa292
Kevin Dias authored
1  Gemfile
View
@@ -1,6 +1,7 @@
source 'https://rubygems.org'
gem 'rails', '3.2.3'
+gem 'bcrypt-ruby', '3.0.1'
group :development, :test do
gem 'sqlite3', '1.3.5'
2  Gemfile.lock
View
@@ -44,6 +44,7 @@ GEM
i18n (~> 0.6)
multi_json (~> 1.0)
arel (3.0.2)
+ bcrypt-ruby (3.0.1)
builder (3.0.0)
coffee-rails (3.2.2)
coffee-script (>= 2.2.0)
@@ -147,6 +148,7 @@ PLATFORMS
ruby
DEPENDENCIES
+ bcrypt-ruby (= 3.0.1)
coffee-rails (~> 3.2.2)
fastercsv (~> 1.5.4)
formtastic (~> 1.2.4)
49 README.md
View
@@ -127,14 +127,18 @@ Now navigate to http://[yourappname].herokuapp.com/surveys
20) Add some basic validations to the User model
class User < ActiveRecord::Base
- attr_accessible :email, :name
+ attr_accessible :name, :email, :password, :password_confirmation
+ has_secure_password
before_save { |user| user.email = email.downcase }
- validates :name, presence: true, length: { maximum: 50 }
+ validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
- validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
+ validates :email, presence: true,
+ format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
+ validates :password, length: { minimum: 6 }
+ validates :password_confirmation, presence: true
end
21) Add an index to the user email
@@ -149,19 +153,50 @@ Now navigate to http://[yourappname].herokuapp.com/surveys
end
end
-23) Migrate the database
+23) Add password_digest to the model
+
+ $ rails generate migration add_password_digest_to_users password_digest:string
+
+24) Migrate the database
$ bundle exec rake db:migrate
-24) Open the rails console and create a new user
+25) Open the rails console and create a new user
$ rails console
- >> User.create(name: "Your Name", email: "yourname@example.com")
+ >> User.create(name: "Your Name", email: "yourname@example.com",
+ ?> password: "foobar", password_confirmation: "foobar")
-25) Commit the changes
+26) Commit the changes
$ git add .
$ git commit -m "A basic User model"
+ $ git push
+ $ git push heroku
+
+###Section 4 - Create a sign up and sign in form
+
+27) Add a Users resource to the routes file
+
+ SurveyorExample::Application.routes.draw do
+ resources :users
+ end
+
+be sure to remove the line 'get "users/new"'
+
+28) Create a form to sign up new users
+
+Edit this view: app/views/users/new.html.erb
+
+You can copy the code from [this example](https://github.com/diasks2/surveyor_example/blob/master/app/views/users/new.html.erb)
+
+29) Add an @user variable to the new action
+
+ class UsersController < ApplicationController
+ def new
+ @user = User.new
+ end
+ end
1  app/controllers/users_controller.rb
View
@@ -1,4 +1,5 @@
class UsersController < ApplicationController
def new
+ @user = User.new
end
end
12 app/models/user.rb
View
@@ -1,10 +1,14 @@
class User < ActiveRecord::Base
- attr_accessible :email, :name
+ attr_accessible :name, :email, :password, :password_confirmation
+ has_secure_password
before_save { |user| user.email = email.downcase }
- validates :name, presence: true, length: { maximum: 50 }
+ validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
- validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
+ validates :email, presence: true,
+ format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
-end
+ validates :password, length: { minimum: 6 }
+ validates :password_confirmation, presence: true
+end
21 app/views/users/new.html.erb
View
@@ -1,2 +1,19 @@
-<h1>Users#new</h1>
-<p>Find me in app/views/users/new.html.erb</p>
+<h1>Sign Up</h1>
+
+ <%= form_for(@user) do |f| %>
+
+ <%= f.label :name %>
+ <%= f.text_field :name %>
+
+ <%= f.label :email %>
+ <%= f.text_field :email %>
+
+ <%= f.label :password %>
+ <%= f.password_field :password %>
+
+ <%= f.label :password_confirmation, "Confirmation" %>
+ <%= f.password_field :password_confirmation %>
+
+ <%= f.submit "Create my account" %>
+
+ <% end %>
2  config/routes.rb
View
@@ -1,5 +1,5 @@
SurveyorExample::Application.routes.draw do
- get "users/new"
+ resources :users
# The priority is based upon order of creation:
# first created -> highest priority.
5 db/migrate/20120427001716_add_password_digest_to_users.rb
View
@@ -0,0 +1,5 @@
+class AddPasswordDigestToUsers < ActiveRecord::Migration
+ def change
+ add_column :users, :password_digest, :string
+ end
+end
7 db/schema.rb
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120426233439) do
+ActiveRecord::Schema.define(:version => 20120427001716) do
create_table "answers", :force => true do |t|
t.integer "question_id"
@@ -169,8 +169,9 @@
create_table "users", :force => true do |t|
t.string "name"
t.string "email"
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ t.string "password_digest"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
Please sign in to comment.
Something went wrong with that request. Please try again.