Posts

Showing posts from 2020

Faraday::SSLError: SSL_connect returned=1 errno=0 state=error: certificate verify failed (certificate has expired)

  require 'openssl' OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE put this lines in config/application.rb

unterminated string meets end of file +rails

 This error is because you are trying to instert special symbol in record. User.last.update(password: "!@#$") User.last.update(password: "!@/#$") //it will not throw any errors.

The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of Array

 res.end() to res.send()

Some Ruby on Rails interview questions

 1) How flash message work in ruby on rails? Ans. They are stored in your session store. The default since rails 2.0 is the cookie store, but check in config/initializers/session_store.rb to check if you're using something other than the default. According to APIdock : ActionController/Flash, it is stored in a session. Note that if sessions are disabled only flash.now will work. When using flash.now, your values are not available in the next request. 2) What is the use of Asset-PipeLine in Rails? Ans. Assets(css, scss, js, coffeescript, images etc), its fast executed in rails application because of asset pipeline when we compile it or when its compiled, it will generate a single source code file or a manifest file, so that's why assets are interpret fastly. 3)   Difference between find and find_by? Ans . The find method is usually used to retrieve a row by ID:            Model.find(1)           And it will throw ActiveRecord::RecordNotFound error if record not found. find_by is

Some important and unknown(least used) ruby methods.

 1) inject or reduce      When called, the inject method will pass each element and accumulate each sequentially.     ex:         [1,2,3].inject(:+)    => ((1+2)+3))   //we have can any operator here i.e +,-,*,/,%         OR         [1,2,3].inject{|sum, value| sum + value } => 1 iteration:  (1 + 2)                                                                                             =>  2 iteration: (3(1st iteration sum)+3)            we  can also pass it a default value or base value for the accumulator.     ex:          [1,2,3].inject(0, :+)    => (0+(1+2)+3)) //so here initial value for sum is 0.          We can also use inject menthods for building hash's from the array's     ex:           [[:book, "data structure"], [:price, "400rs"]].inject({}) do |result, element|               result[element.first] = element.last               result           end            #=> {:book=>"data structure", :price=>"400rs&qu

Setup a cron job or scheduler for your rails application using whenever gem.

cd path to your project. gem 'whenever', require: false   add it to your gemfile run bundle install bundle exec wheneverize .   //This will create an initial config/schedule.rb Now create your job call it  from config/schedule.rb whenever --update-crontab   //update crontab crontab -l   //list all cron job set :output, "log/cron.log"    //if you want to set log file for cron job then that this line at the top of config/schedule.rb         in production server:                   after deployment :  bundle exec whenever whenever --update-crontab crontab -l source: https://github.com/javan/whenever https://www.rubyguides.com/2019/04/ruby-whenever-gem/

Setup ruby god for rails application || Daemons setup for rails application

 1) Install god on Ubuntu/Linux machine      sudo apt update      sudo apt install ruby-god 2) Create new rails application if you don't have existing 3) In your Gemfile add new gem       gem 'god', '~> 0.13.7', require: false 4) Then run bundle install 5) create a folder named daemons inside your  lib folder 6) create a file name daemons.god inside daemons folder (this file name or folder name can be anything its totally up to you) 7) Then paste this on you daemons.god file      ENV["RAILS_ENV"] = "development"      RAILS_ENV  = ENV.fetch('RAILS_ENV', 'production')      RAILS_ROOT = File.expand_path('../../..', __FILE__)      require 'shellwords'      # Create non-default log/daemons directory.      require 'fileutils'      FileUtils.mkdir_p "#{RAILS_ROOT}/log/daemons"      def daemon(name, options = {})        God.watch do |w|          command        = "bundle exec ruby lib/daemons/#{op

How to upgrade from Ubuntu Linux 16.04 to 18.04

Image
Type the apt command or apt-get command to upgrade existing system:   sudo apt update   sudo apt upgrade Reboot the Linux box if kernel was updated   sudo reboot If you have a ufw firewall running on ufw, open ssh port 1022 using the following ufw syntax:   sudo ufw allow 1022/tcp comment 'Temp open port ssh tcp port 1022 for upgrade' Upgrade the operating system to the latest release from the command-line by typing the following command:   sudo do-release-upgrade After some time you will see “System upgrade is complete” message. Press y to restart your system to load updates kernel and operating system for your cloud server: Verification Login using the ssh command:   ssh user@your-ubuntu-server Verify Ubuntu Linux kernel version with uname:   uname -r Make sure all needed ports are open and running with the ss command or netstat command:   sudo ss -tulpn   sudo netstat -tulpn Use grep command/egrep command or tail command/cat command to verify log files for errors:    dmesg  

Copy a text of div by button click by javascript

Your html code  <input type="button" id="copyme" value="COPY THE DIV"> <div id="mypolicy">THIS IS WHAT I WANT TO COPY</div> js script $('#copyme').on('click', function(){     id = this.id     var range = document.createRange();     range.selectNode(document.getElementById(id));     window.getSelection().removeAllRanges();   // clear current selection     window.getSelection().addRange(range);   // to select text     document.execCommand("copy");   });

Revert pushed commit from git

Image
There are 2 way to revert pushed code using git commands 1) First Way, Let suppose your current branch name is staging and you want to revert commit from staging git log   you will see commit details   now git checkout <commit_hash_where_you_want_to_point_your_head>                 in this picture i want to remove 1st commit                 so my command will be git checkout a804a3c4662334b0aa33860548841db54fb508bb         3. Now Head will be on second commit so create new branch from here git checkout -b staging_copy (name will be depends on you)      4. Now staging_copy branch does not have reverted commit, means its code is clean       5. Now delete your staging branch from local machine git branch -D staging    6. now rename your staging_copy branch to staging git branch -M staging    7. now we need to force push to staging branch git push origin staging -f This process is lengthy and complex but we are using this because in this proc

cannot find module angular http

npm install @angular/http@latest

How to Access RabbitMQ Management Plugin rabbitmqadmin Command Line

1) Enable rabbitmq_management   sudo rabbitmq-plugins enable rabbitmq_management 2) Restart rabbitmq-server sudo service rabbitmq-server restart 3) Access GUI http://rabbitmq-server-ip:15672/ username:guest password:guest //you can check ip sudo service rabbitmq-server status command 4) Access  rabbitmqadmin Command Line http://rabbitmq-server-ip:15672/cli a) Download  rabbitmqadmin script wget http://rabbitmq-server-ip:15672/cli/rabbitmqadmin b) Change permission chmod +x rabbitmqadmin 5) rabbitmqadmin Commands a) Help ./rabbitmqadmin --help b)List all exchanges ./rabbitmqadmin -u guest -p cloud -H localhost list exchanges -u : username -p : password c) List all queues   ./rabbitmqadmin -u guest -p cloud -H localhost list queues d) Get help of subcommands ./rabbitmqadmin help subcommands ./rabbitmqadmin help subcommands | grep queue ./rabbitmqadmin help subcommands | grep exchnage ./rabbitmqadmin help subcommands | grep binding

How to Install or Uninstall Redis on Ubuntu 18.04

Step 1 – Open the terminal step 2. Update APT Repository Cache   (1)check the version table on your operating system     The package you are searching for is named redis-server. You can check its package info with apt show redis-server sudo apt-cache policy redis-server or sudo apt show redis-server The dataset is stored entirely in memory and periodically flushed to disk. (2)add PPA repository to your OS: sudo add-apt-repository ppa:chris-lea/redis-server (3)In order to install Redis, you first need to update the APT repository cache of your Ubuntu. You can do that with the following command: Update the apt-get packages index files and also update existing packages to the newest versions by using the following commands: sudo apt-get update  sudo apt-get upgrade step 2. Install Redis on Ubuntu Using the APT Command sudo apt-get install redis-server step 3. Check Redis Version redis-cli --version step 4:check if the Redis is running. Once t

lambda proc and block in rails

this all things basically we used in block level coding and for some custom queries in rails model class Main   [ 1 , 2 , 3 ].each do | num |     puts num   end end def block_method   yield end block_method { puts "Test"} def arg_method   yield   yield 1 end arg_method { |i| puts i*1 rescue nil} # explicit block def explicit_block(&block)   block.call #same as yield end explicit_block {puts "This is explicit block"} # lambda lambda_test = -> {puts "This is lambda"} lambda_test.call lambda_test.() lambda_test[] # lambda with args arg_lambda = -> (i){ puts i} arg_lambda.call("test") my_proc = Proc.new { |x| puts x } my_proc.call # Should work my_lambda = -> { return 1 } puts "Lambda result: #{my_lambda.call}" # Should raise exception my_proc = Proc.new { return 1 } puts "Proc result: #{my_proc.call}" def call_proc   puts "Before proc"   my_proc = Proc.n

unable to install ruby 2.2 in ubuntu 18.04

Try with this commands 1) Install rvm following instructions for Ubuntu on  https://github.com/rvm/ubuntu_rvm 2) rvm install ruby 2.2.10 3) rvm install ruby 2.2.10 --with-gcc=gcc if its not work then try with this  1. sudo apt purge libssl-dev && sudo apt install libssl1.0-dev 2. rvm install 2.3.5 --autolibs=disable

rails redirect after login devise

devise_scope :user do  authenticated :user do    root 'dashboard#index', as: :authenticated_root  end  unauthenticated do    root 'home#index', as: :unauthenticated_root  end end