[Ruby On Rails] Authentication – has_secure_password Feature (2) – Signup

This is the second part of the “has_secure_password” Feature in Rails. This post explains how to create a “signup” page. Please refer to the previous post (Overview) to setup the feature.

1. Add Controller

To manange users, let’s create the “Users” controller.

> rails g controller Users

 

2. Configure Routes

In the “config/routes.rb” file, add the following code.

resources :users

 

3. Signup Link

Signup is just a Restful service to create a user object.

<%= link_to "Sign Up", new_user_path, class: "btn btn-primary" %>

 

4. Add Actions

Add the “new” and “create” actions in the “Users” controller.

class UsersController < ApplicationController

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      flash[:success] = "A new user is created successfully"
      redirect_to @user
    else
      render "new"
    end
  end

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

end

 

5. Signup form

Add the “new.html.erb” file to the “views/users” folder.

</pre>
<h2>Sign up</h2>
<div class="row">
  <div>
    <%= form_for(@user, html: { class: "form-horizontal" }) do |f| %>

      <%= render 'error' %>

      <div class="form-group">
        <%= f.label :name, class: "col-sm-2 control-label" %>
        <div class="col-sm-6"><%= f.text_field :name %></div>
      </div>
      <div class="form-group">
        <%= f.label :email, class: "col-sm-2 control-label" %>
        <div class="col-sm-6"><%= f.text_field :email %></div>
      </div>
      <div class="form-group">
        <%= f.label :password, class: "col-sm-2 control-label" %>
        <div class="col-sm-6"><%= f.password_field :password %></div>
      </div>
      <div class="form-group">
        <%= f.label :password_confirmation, "Confirmation", class: "col-sm-2 control-label" %>
        <div class="col-sm-6"><%= f.password_field :password_confirmation %></div>
      </div>
      <div class="form-group">
        <div class="col-sm-offset-2 col-sm-6"><%= f.submit "Create Account", class: "btn btn-large btn-primary" %></div>
      </div>
    <% end %>
  </div>
</div>

 

6. Display Errors

Add the “_error.html.erb

<% if @user.errors.any? %>
<div class="alert alert-warning" id="error_explanation">
  <h3><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h3>
  <ul>
    <% @user.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
  </ul>
</div>
<% end %>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s