Tuesday, 9 October 2018

404 page

Please visit site:

https://mattbrictson.com/dynamic-rails-error-pages

rails generate controller errors not_found internal_server_error
This creates app/controllers/errors_controller.rb with corresponding view templates in app/views/errors/ for the not found (404) and internal server error (500) pages. (Ruby methods declared with def can’t start with numbers, so use the spelled-out error names instead.)

2SEND THE RIGHT STATUS CODES

class ErrorsController < ApplicationController
  def not_found
    render(:status => 404)
  end

  def internal_server_error
    render(:status => 500)
  end
end
Normally Rails sends an HTTP status of 200 OK when it renders a view template, but in this case we want 404 or 500 to be sent according to error page being rendered. This requires a slight tweak to the errors_controller.rb that Rails generates. You don’t need to specify the name of the template to render, because by convention it is the same as the action name.

3CONFIGURE THE ROUTES

match "/404", :to => "errors#not_found", :via => :all
match "/500", :to => "errors#internal_server_error", :via => :all
Rails expects the error pages to be served from /<error code>. Adding these simple routes in config/routes.rb connects those requests to the appropriate actions of the errors controller. Using match ... :via => :all allows the error pages to be displayed for any type of request (GET, POST, PUT, etc.).

4TELL RAILS TO USE OUR ROUTES FOR ERROR HANDLING

config.exceptions_app = self.routes
Add this line to config/application.rb. This tells Rails to serve error pages from the Rails app itself (i.e. the routes we just set up), rather than using static error pages in public/.

5. config/environments/development.rb:
config.consider_all_requests_local = false
























Thursday, 4 October 2018

Login with username

Please visit site:

https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign-in-using-their-username-or-email-address

Create a migration:

rails generate migration add_username_to_users username:string:uniq

Run the migration:

rake db:migrate
User.rb:

devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable, authentication_keys: [:login]

attr_writer :login

validates :username, presence: :true, uniqueness: { case_sensitive: false }
  validate :validate_username

def login
    @login || self.username || self.email
  end
  def self.find_for_database_authentication(warden_conditions)
    conditions = warden_conditions.dup
    if login = conditions.delete(:login)
      where(conditions.to_h).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
    elsif conditions.has_key?(:username) || conditions.has_key?(:email)
      where(conditions.to_h).first
    end
  end
  def validate_username
    if User.where(email: username).exists?
      errors.add(:username, :invalid)
    end
  end
=========================================================

Modify config/initializers/devise.rb to have:
config.authentication_keys = [ :login ]

Application.rb

config.exceptions_app = self.routes

Modify the views

sessions/new.html.erb:
  -  <p><%= f.label :email %><br />
  -  <%= f.email_field :email %></p>
  +  <p><%= f.label :login %><br />
  +  <%= f.text_field :login %></p>
Use :login and not :username in the sessions/new.html.erb
registrations/new.html.erb:
  +  <p><%= f.label :username %><br />
  +  <%= f.text_field :username %></p>
     <p><%= f.label :email %><br />
     <%= f.email_field :email %></p>
registrations/edit.html.erb:
  +  <p><%= f.label :username %><br />
  +  <%= f.text_field :username %></p>
     <p><%= f.label :email %><br />
     <%= f.email_field :email %></p>

Configure Devise to use username as reset password or confirmation keys

Simply modify config/initializers/devise.rb to have:
config.reset_password_keys = [ :username ]
config.confirmation_keys = [ :username ]

















Password update

Routes.rb :

resources :users do
    collection do
      patch 'update_password'
    end
  end
  get 'change_password' => "users#change_password"


users_controller.rb :

def change_password
    @user = current_user
  end
  def update_password
    @user = current_user
    unless @user.valid_password?(params[:user][:current_password])
      @user.errors.add(:base, "Current Password is incorrect.")
      return render "change_password"
    end
    if @user.update(user_params)
      # Sign in the user by passing validation in case their password changed
      bypass_sign_in(@user)
      redirect_to root_path
      #redirect_to destroy_user_session_path
    else
      render "change_password"
    end
  end