From c3d1c6aadb73e5cfcfdfc549e31e1cae7bd1eed7 Mon Sep 17 00:00:00 2001 From: Kevin Date: Fri, 27 Apr 2012 11:56:41 +0900 Subject: [PATCH] Signup form --- Gemfile | 1 + Gemfile.lock | 2 + README.md | 49 ++++++++++++++++--- app/controllers/users_controller.rb | 1 + app/models/user.rb | 12 +++-- app/views/users/new.html.erb | 21 +++++++- config/routes.rb | 2 +- ...0427001716_add_password_digest_to_users.rb | 5 ++ db/schema.rb | 7 +-- 9 files changed, 83 insertions(+), 17 deletions(-) create mode 100644 db/migrate/20120427001716_add_password_digest_to_users.rb diff --git a/Gemfile b/Gemfile index 4bd779a..6a192ab 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index 515df60..10c5484 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) diff --git a/README.md b/README.md index 6c0dbd5..79f8fd5 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 2ec9ecc..fec5a3f 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,4 +1,5 @@ class UsersController < ApplicationController def new + @user = User.new end end diff --git a/app/models/user.rb b/app/models/user.rb index 7b353a7..18058f9 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 \ No newline at end of file diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb index c21a1ad..9901fd4 100644 --- a/app/views/users/new.html.erb +++ b/app/views/users/new.html.erb @@ -1,2 +1,19 @@ -

Users#new

-

Find me in app/views/users/new.html.erb

+

Sign Up

+ + <%= 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 %> diff --git a/config/routes.rb b/config/routes.rb index e2e24ce..3889e54 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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. diff --git a/db/migrate/20120427001716_add_password_digest_to_users.rb b/db/migrate/20120427001716_add_password_digest_to_users.rb new file mode 100644 index 0000000..7ad1f62 --- /dev/null +++ b/db/migrate/20120427001716_add_password_digest_to_users.rb @@ -0,0 +1,5 @@ +class AddPasswordDigestToUsers < ActiveRecord::Migration + def change + add_column :users, :password_digest, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 2e6c86a..1698024 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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