Monday, 5 August 2019

Upload image using ajax request in rails

          <%=  file_field_tag :customer_profile, id: "customer_profile"  %>           

          var formData = new FormData();
          custProfile  =$("#customer_profile")[0].files[0]
          formData.append("device_img", custProfile);
          $.ajax({
          url: "<%=  upload_image_customer_path(id: params[:id]) %>",
          type: 'POST',
          dataType: 'json',
          data: formData,
          contentType: false,
          processData: false,
          cache : false,
          success: function(data){
            if(data["msg"] == true){
              alert(success)
             }
            },
           error: function(data){
             console.log(data); 
            }
          });

Friday, 2 August 2019

Image convert in base64 in rails

require "base64"

file_url = "https://s3.ap-south-1.amazonaws.com/on-store-billing/uploads/insurance/servify_device_image/58/1__2_.jpeg"

encoded_string = Base64.encode64(open(file_url) { |io| io.read })

decode_img= Base64.decode64(encoded_string)

below given method write/create image with name image.png in directory

File.open(‘image.png’, ‘wb’) { |f| f.write(decode_img) }

Tuesday, 30 July 2019

Login Postgresql with specific database

$  psql -d db_name -U username

e.g. $  psql -d on_store_development -U postgres

then it will ask for password default is root or postgres. Or check your database.yml for a password that password you have to type here.

Tuesday, 25 June 2019

Important commands in rails and rake

Rails:
  console
  credentials:edit
  credentials:show
  dbconsole
  destroy
  encrypted:edit
  encrypted:show
  generate
  new
  runner
  secrets:edit
  secrets:setup
  secrets:show
  server
  test
  version

Rake:
  about
  active_storage:install
  app:template
  app:update
  assets:clean[keep]
  assets:clobber
  assets:environment
  assets:precompile
  cache_digests:dependencies
  cache_digests:nested_dependencies
  db:create
  db:drop
  db:environment:set
  db:fixtures:load
  db:load_config
  db:migrate
  db:migrate:status
  db:rollback
  db:schema:cache:clear
  db:schema:cache:dump
  db:schema:dump
  db:schema:load
  db:seed
  db:setup
  db:structure:dump
  db:structure:load
  db:version
  dev:cache
  initializers
  log:clear
  middleware
  notes
  notes:custom
  restart
  routes
  secret
  stats
  test
  test:db
  test:system
  time:zones[country_or_offset]
  tmp:clear
  tmp:create
  yarn:install

Friday, 24 May 2019

Copy Dump from server to local machine


~/Desktop$ scp -i ~/Desktop/permission/h2w/postgres-dump.pem ubuntu@0.0.0.0:./dumpfile_name ./

Friday, 26 October 2018

Create Dynamic CSV with custom data, Stored in tmp directory and upload to AWS S3 Bucket without any rails uploder method.

1. Add gem "aws-sdk-s3" and bundle install
 
  require 'aws-sdk-s3'
  # AWS S3 bucket upload method
  def self.upload_csv_to_s3_bucket
    s3 = Aws::S3::Resource.new(region: REGION_S3, access_key_id: ACCESS_KEY_ID_S3, secret_access_key: SECRECT_ACCESS_KEY_S3)
    file = "#{Rails.root}/tmp/file_name_#{Date.today}.csv"
    name = File.basename(file)
    bucket = BUCKET_S3
    obj = s3.bucket(bucket).object(name)
    metadata = {"answer" => "42"}
    obj.upload_file(file, metadata: metadata)
    # this below line of code used to print file name and S3 File ULR
    bucket = s3.bucket(bucket)
    bucket.objects.limit(50).each do |item|
      puts "Name:  #{item.key}"
      puts "URL:   #{item.presigned_url(:get)}"
    end
  end
 
  def self.csv_generate(header, total_with_times, account_id, file_name)
    begin
    result = CSV.generate(headers: true) do |csv|
      csv << header
      total_with_times.each do |val|
        csv << val
      end
    end
    # Delete old CSV file for creating new one
    File.delete("#{Rails.root}/tmp/#{ file_name }_#{ Date.today - 1.days }.csv") rescue ''
    # Creating new CSV file for all accounts
    File.open("#{Rails.root}/tmp/#{ file_name }_#{ Date.today }.csv", 'w:UTF-8') { |file| file.write(result)}
    upload_csv_to_s3_bucket
  rescue Exception => e
    puts "<<<>>>>>> | #{e.message}| <<<<<<<>>>>>"
  end
  

Revert last commit or second last and more....

 Git revert commit_id -m 1 this command willl revert last commit  Git revert commit_id -m 2 this command will revert second commit with same...