Posts

Showing posts from November, 2019

some minor things in rails

For Date format  .utc.strftime('%d-%m-%Y %H:%M:%S') Alternative to html_safe (raw("<strong class='text-danger'>Decline</strong")). select tag for form_for  <%= f.select(:currency_id, options_for_select(@currencies.map{|x| x.id}, f.object.payment_currency), {include_blank: "Select Currency"}, { :class => 'form-control', required: true }) %> zip -r new_file_name.zip folder_name

Convert numbers in rails like binary to decimal

value.to_i(from).to_s(to) i.e "1000".to_i(2).to_s(10) //here we are converting binary to decimal

decimal to binary converter in rails

def dec2bin(number)     number = Integer(number) //number that you have to convert     if(number == 0) then 0 end              new_num = ""     while(number != 0)         new_num = String(number % 2) + new_num         number = number / 2     end     new_num // return new number end

Authentication via JWT in rails api's

gem 'jwt' add this gem to your gem file bundle install create a file in lib/json_web_token.rb and paste following code class JsonWebToken      SECRET_KEY = Rails.application.secrets.secret_key_base. to_s      def self.encode(payload, exp = 15.days.from_now)         payload[:exp] = exp.to_i        JWT.encode(payload, SECRET_KEY)      end     def self.decode(token)        decoded = JWT.decode(token, SECRET_KEY)[0]        HashWithIndifferentAccess.new decoded     end end create another file config/initializers/jwt.rb and paste following code require 'json_web_token' Now inside controller/api/v1/ we will a parent class for our all end-points: api/v1/api_controller.rb class Api::V1::ApiController < ActionController::Base      protect_from_forgery with: :null_session      def autheticate_user         header = request.headers['Authorization']         header = header.split(' ').last if header         begin             @decoded = JsonWebToken.decode

How to use switch case in angular view

      <div ng-switch =" varible_name ">           <div ng-switch-when ="value1"><p>{{ value1}}</p></div>           <div ng-switch-when ="value2"><p>{{ value2 }}</p></div>           <div ng-switch-when ="value3"><p>{{ value3 }}</p></div>           <div ng-switch-when ="value4"><p>{{ value4}}</p></div>           <div ng-switch-default >...</div>       </div>