March 03, 2015

Home » » Sign up On Rails App - Create A Sample Web Application On Rails Part03

Sign up On Rails App - Create A Sample Web Application On Rails Part03

Posted by Unknown
0

 In this series I will create a simple web application. It includes the following main components: 

  • Mostly static pages, use bootstrap
  • Users can signin, signout, remember user signin
  •  Home page show all users, update their profiles, admin users can delete other users.
  • User can following or unfollow other users
  • Users can post articles, update and destroy their posts.
  • Users login and make their comments on articles.
  • Home page show all articles and current user profile, paging.
  • Build url for article
  • Simple search articles
  • Type ahead, use gon gem.

Ok, start!

Part III, Sign up on rails application

1. Create User model

$ rails g model User name:string email:string
$ bundle exec rake db:migrate

Simple validates and format email

Open app/models/user.rb and add:
  validates :name,  presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255 },
                    format: { with: VALID_EMAIL_REGEX }

Uniqueness validation email

Open again app/models/user.rb and edit:
  before_save { self.email = email.downcase }
  validates :name,  presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255 },
                    format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }

Database indices

$ rails generate migration add_index_to_users_email
Open db/migrate/[timestamp]_add_index_to_users_email.rb and edit
class AddIndexToUsersEmail < ActiveRecord::Migration
  def change
    add_index :users, :email, unique: true
  end
end
Run command to migrate database
# rake db:migrate

Secure password

Add password_digest to user model
# rails generate migration add_password_digest_to_users password_digest:string
# rake db:migrate
Open Gemfile and add
gem 'bcrypt',  '3.1.7'
Run command to install gem
# bundle install
Add has secure password and validates to user.rb
  has_secure_password
  validates :password, length: { minimum: 6 }

User sign up

Add user resources to config/routes.rb
  resources :users
resources :users => full restfull
/users 
/users/1
/users/1/edit
/users
/users/new
/users/1
/users/1
Saw full url with these method, run command
# rake routes

Create users controller

# rails g controller users index new show
Create app/views/users/show.html.erb
<%= @user.name %>, <%= @user.email %>
Open app/controllers/users_controller.rb and edit
def show
  @user = User.find(params[:id])
end

def new
  @user = User.new
end

Create sign up form

Open app/views/users/new.html.erb
<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for(@user) do |f| %>
      <%= render 'shared/error_messages' %>

      <%= f.label :name %>
      <%= f.text_field :name, class: 'form-control' %>

      <%= f.label :email %>
      <%= f.email_field :email, class: 'form-control' %>

      <%= f.label :password %>
      <%= f.password_field :password, class: 'form-control' %>

      <%= f.label :password_confirmation, "Confirmation" %>
      <%= f.password_field :password_confirmation, class: 'form-control' %>

      <%= f.submit "Create my account", class: "btn btn-primary" %>
    <% end %>
  </div>
</div>

Open users_controller.rb

  
  def create
   @user = User.new(user_params)
   if @user.save
    flash[:success] = "Sign up successful"
    redirect_to @user
   else
    render 'new'
   end
  end

  private

    def user_params
   params.require(:user).permit(:name, :email, :password, :password_confirmation)
    end

Create error messages

Create app/views/shared/_error_messages.html.erb
<% if @user.errors.any? %>
  <div id="error_explanation">
    <div class="alert alert-danger">
      The form contains <%= pluralize(@user.errors.count, "error") %>.
    </div>
    <ul>
    <% @user.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>
Run commands
# rake db:migrate
# rails s

Try sign up

Fill all and sign up will success.






0 comments:

Post a Comment

Popular Posts

Labels

Archive

 

Blogroll

Recepies

Flickr Images

Copyright © 2014. Tutorials Blog - All Rights Reserved