Posts

Showing posts from June, 2017

CHECK FILE EXTENSION AT THE TIME OF UPLOADING

We want to check file extension before it is uploaded. The action should be executed all in the browser without using Ajax when we upload any file. Associated HTML:<%= f.file_field :file, required: true, :id => "shot_file" %>

<span>(pdf,doc,docx,ppt,pptx,ods,odp,odt)</span>
<div>
    <span class='file_ext_error text-danger'></span>
</div>
Please take a look at the sample JS code below: <script>
  $(document).on('change','#shot_file', function(){
    file_name = $(this).val();
    ext = file_name.split('.');
    ext = ext[ext.length - 1];
    valid_ext = ['pdf','msword','doc','docx','ppt','pptx','ods','odp', 'odt', 'xlsx']
    if (valid_ext.indexOf(ext) >= 0) {
      $('.actions input').removeAttr("disabled");
      $('.file_ext_error').html('');
    }
    else {
      $('.actions input'…

PREVIEW AN IMAGE BEFORE IT IS UPLOADED

I want to be able to preview a file (image) before it is uploaded. The preview action should be executed all in the browser without using Ajax to upload the image. Please take a look at the sample JS code below:
function readURL(input) {

    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $('#blah').attr('src', e.target.result);
        }

        reader.readAsDataURL(input.files[0]);
    }
}


$("#imgInp").change(function(){
    readURL(this);
});
and the associated HTML:
<form id="form1" runat="server">
    <input type='file' id="imgInp" />
    <img id="blah" src="#" alt="your image" />
</form>

WHENEVER WITH CAPISTRANO

When we deploy the project using capistrano , So we want to run the whenever scheduler after deploying the project . So we can follow simple below steps:-

In our capfile :-

require "whenever/capistrano"

Deploy.rb:-

namespace :deploy do
  desc "Update crontab with whenever"
  task :update_cron do
    on roles(:app) do
      within current_path do
        execute :bundle, :exec, "whenever --update-crontab #{fetch(:application)}"
      end
    end
  end
  after :finishing, 'deploy:update_cron'
end


WE CAN REFER :-

http://www.lugolabs.com/articles/14-schedule-rails-tasks-with-whenever-and-capistrano

PROGRESS BAR IN RAILS WITH DELAYED JOBS

Image
Progress Bar in Rails Delayed Jobs :- I am giving you some steps for showing the Jobs Progress Bar without reloading the page :- route.rbmap.resources :jobs, :member=>{:job_status => :get}

Controller :-

class JobsController < InheritedResources::Base
    def job_status
    @job = Delayed::Job.find(params[:id])
    respond_to do |format|
      format.json do
        render :json => @job
      end
    end
  end
end
Add new field in Delayed job file :-class Addtofielddelayedjob < ActiveRecord::Migration
  def self.up
      add_column :delayed_jobs, :max_job, :string, :default => ''
    add_column :delayed_jobs, :current_job, :string, :default => ''
    add_column :delayed_jobs, :job_status, :string
  end

  def self.down
      remove_column :delayed_jobs, :max_job
    remove_column :delayed_jobs, :current_job
    remove_column :delayed_jobs, :job_status
  end
end


Library file :-class NewJob
    def perform
        datas.each_with_index do |data, index|
            content....…

/BIN/BASH: BUNDLE: COMMAND NOT FOUND

Image
When we get error on the server :-Rails Cron Whenever gem error, bundle: command not found /bin/bash: bundle: command not found Then,We need to use PATH and BUNDLE COMMAND  in the scheduler file config/scheduler.rbenv :PATH, ENV['PATH']
set :bundle_command, "/usr/local/bin/bundle exec"


set :environment, "production" set :output, {:error => "log/cron_error_log.log", :standard => "log/cron_log.log"}
every 2.minute do    runner "Delayed::Job.enqueue(UpdateNetwinstProductJob.new)" end
https://stackoverflow.com/questions/26509418/rails-cron-whenever-gem-error-bundle-command-not-found

WHENEVER SCHEDULER

Image
https://github.com/javan/whenever
https://www.sitepoint.com/schedule-cron-jobs-whenever-gem/

Gemfile:-

gem 'whenever', :require => false

For creating scheduler.rb file :- Run that command

wheneverize .

# file: config/schedule.rb

set :environment, "development"
set :output, {:error => "log/cron_error_log.log", :standard => "log/cron_log.log"}

For running the Rake task use:-every 1.minute do rake "shop_products:shop_product_upload_url" endFor running the Sidekiq Worker :-every 1.minute do runner "UpdateShopProductWorker.perform_async" endRake task:-

lib/tasks/shop.rake

namespace :shop_product do
  task :shop_url => :environment do
    ShopProductUploadUrl.all.each do |csv_data|
       # our operation
    end
  end
end
Worker:- worker/updateshopproductworker.rb

class UpdateShopProductWorker
    include Sidekiq::Worker
    LOGGER = Logger.new("#{Rails.root}/log/cron_log.log")

    def perform
        update_shop_products
    end

   …

Faraday::ConnectionFailed: Connection refused - connect(2)

When we get some elastic search 9200 port problem :-Faraday::ConnectionFailed: Connection refused - connect(2) for "domainname.com" port 9200
For solving that issue, open the file

     sudo nano /etc/elasticsearch/elasticsearch.yml

and update the network host:-

     network.host: 0.0.0.0

Then restart the elastic search :-

     sudo service elasticsearch restart

Then your console you can run the command :-

    Product.reindex

WE CAN REFER :-

https://stackoverflow.com/questions/31677563/connection-refused-error-on-elastic-search