Skip to content

Commit

Permalink
Signup form
Browse files Browse the repository at this point in the history
  • Loading branch information
diasks2 committed Apr 27, 2012
1 parent c0aa292 commit c3d1c6a
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 17 deletions.
1 change: 1 addition & 0 deletions 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'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
49 changes: 42 additions & 7 deletions README.md
Expand Up @@ -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
Expand All @@ -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 change: 1 addition & 0 deletions app/controllers/users_controller.rb
@@ -1,4 +1,5 @@
class UsersController < ApplicationController
def new
@user = User.new
end
end
12 changes: 8 additions & 4 deletions 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
21 changes: 19 additions & 2 deletions app/views/users/new.html.erb
@@ -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 changes: 1 addition & 1 deletion 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.
Expand Down
5 changes: 5 additions & 0 deletions 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
7 changes: 4 additions & 3 deletions db/schema.rb
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c3d1c6a

Please sign in to comment.