I have created a signup and login form in a simple and user-friendly manner, ensuring that the process is easy to understand and follow. I used Bootstrap and HTML with embedded Ruby (ERB) to build the form, following the MVC (Model-View-Controller) structure. The form is connected to a database, allowing seamless data storage and retrieval.
Commands :
#console
rails g controller users
rails g controller sessions
rails g model User name:string email:string password_digest:string
Routes :
root "sessions#new"
get 'login', to: 'sessions#new'
post 'login', to:'sessions#create'
delete 'logout', to:'sessions#destroy'
resources :users, only: [:new, :create]
get 'signup', to: 'users#new'
Model :
#apps/models/user.rb
class User < ApplicationRecord
has_secure_password
validates :name, presence: true
validates :email, presence: true
validates :password, presence: true, length: { minimum: 6 }
end
Controller for sign-up:
#app/controllers/users_controller.rb
#Signup form
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
session[:user_id] = @user.id
# flash[:success] = "Account created! Log in to begin."
redirect_to login_path
else
flash.now[:danger] = "Please fix the errors below."
render :new, status: :unprocessable_entity
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
View for sign-up :
#app/views/users/new
<h2>Create an account</h2>
<div class="container">
<% if flash[:success] %>
<div class="alert alert-success">
<%= flash[:success] %>
</div>
<% elsif flash[:danger] %>
<div class="alert alert-danger">
<%= flash[:danger] %>
</div>
<% end %>
</div>
#Creating a form
<%= form_with model: @user, url: users_path, local: true do |f| %>
<div class="field">
<%= f.label :name%>
<%= f.text_field :name, class: "form-control"%>
</div>
<div class="field">
<%= f.label :email %>
<%= f.email_field :email,class: "form-control"%>
</div>
<div class="field" >
<%= f.label :password %>
<%= f.password_field :password , class: "form-control"%>
</div>
<div class="field">
<%= f.label :password_confirmation %>
<%= f.password_field :passsword_confirmation,class: "form-control" %>
</div>
<div class="text-center">
<%= f.submit 'Sign Up', class: "btn btn-primary mt-3" %>
</div>
<% end%>
<p>Already have an account? <%= link_to 'Log In', login_path %></p>
Note: Error Notifications and Required Fields
- Error Notifications: Implement a system to notify users when they haven’t filled in mandatory fields. This can be visual cues like highlighting the missing field or displaying an error message next to it.
- Required Fields: Mark mandatory fields with a clear indication for users. This is commonly done using the required attribute (e.g., required=”true” in HTML) or a visual cue like an asterisk (*).
<div class="field">
<%= f.label :name%>
<%= f.text_field :name, required: true, class: "form-control"%>
</div>
Controller for Log-in:
class SessionsController < ApplicationController
#users an login here
def new
end
def create
user = User.find_by(email: params[:email])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
# flash.now[:success] controllers/
redirect_to pages_path, notice: 'Logged in successfully!'
else
flash.now[:danger] = 'Invalid email or password'
render :new, status: :unprocessable_entity
end
end
def destroy
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
View for Log-in:
<%= form_with url: login_path, local: true do |f| %>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, required: true, placeholder: "Email address", class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :password %>
<%= f.password_field :password,required: true, placeholder: "Password", class: "form-control" %>
</div>
<div class=" text-center" >
<%= f.submit 'Log in', class: "btn btn-primary mt-3" %>
<%= link_to "Forgot password?", new_password_reset_path %>
</div>
<% end %>
<p>Don't have an account? <%= link_to 'Create an Account', signup_path %></p>