Gurad FATAL: Listen error

Tuesday, February 9, 2016 · Posted in ,

While using guard with Rails 4, I have faced an error:

FATAL: Listen error: unable to monitor directories for changes.


Below command in Debian system work fine for me:

echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

I hope this helps.

jQuery Ajax Rails 3 - WARNING: Can't verify CSRF token authenticity

Tuesday, September 17, 2013

 WARNING: Can't verify CSRF token authenticity

This warning appears while I'm trying to do jQuery ajax request.

I have faced this issue while upgrading my older rails app to rails 3.2.

This issue solved by adding bellow lines in head section in layout of application for me.

    <%= csrf_meta_tag %>

    <%= javascript_tag do %>
      jQuery(document).ajaxSend(function(e, xhr, options) {
       var token = jQuery("meta[name='csrf-token']").attr("content");
        xhr.setRequestHeader("X-CSRF-Token", token);
      });
    <% end %>

The session was being reset in each ajax request (GET, POST, PUT, DELETE).
Adding the above mentioned code resolved my issue.

How to convert Time from/to epoch time in ruby

Monday, April 15, 2013 · Posted in , , ,

In ruby, we can find time from/to epoch time as below:

To Epoch Time:

To find current epoch time in seconds (10 digit) use below code,
    current_epoch_time =( Time.now.to_i)
                          or
    current_epoch_time =( Time.now.to_f).ceil

To find current epoch time in mili-seconds (13 digit) use below code,
    current_epoch_time =( Time.now.to_f*1000).ceil

From Epoch Time:

If we want Time from epoch time then use below code,

begin
   Time.at(current_epoch_time)
rescue RangeError => e
   Time.at(
current_epoch_time / 1000)
end


In above code,
1. current_epochtime is in integer.
2. If epochtime is in miliseconds (13 digit) then it will raise RangeError.
3. We have catch RangeError and find datetime accordingly.


I hope it's useful to work with epoch time in ruby.


Fetch Images from Web Url

Wednesday, March 21, 2012 · Posted in ,

You can find images from given web-url by using following code:

For this first you need to write below statements in your .rb file.

require "open-uri"
require "nokogiri"
require "image_size"


now use below code:

  def fetch_images(url)
    return [] if url.nil?
    url_content = open(url)
    url_data = Nokogiri::HTML(url_content)
    images = []
    url_data.xpath("/html/body//img").each do |img|
       src = img["src"]
       full_img_src = src.match(/^(http|https):\/\//) ? src : File.join(url,src)
       if valid_image(full_img_src)
        images << full_img_src
       end
    end
    return images
  rescue
     return 0
  end

  def valid_image(img_url)
    return false if img_url.nil?
    valid_img = false
    img_data = open(img_url, "rb").read
    min_size  = [100,100]
    img_size = ImageSize.new(img_data).size
    if img_size[0] >= min_size[0] and img_size[1] >= min_size[1]
      valid_img = true
    end
    return valid_img
  rescue
    return false
  end

Here I have restricted images to fetch only (100x100) sized images.

multiple database connections in rails

Friday, July 29, 2011

One of my colleague has done multiple db connection in rails


Here is his work,


First set connection parameters  for both database with different constant variables and define in environment file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
DB1 = {
 
:adapter => 'mysql',
 
:database => DATABASE1,
 
:username => USERNAME,
 
:password => PASSWORD,
 
:host => HOST
 
}
 
DB2 = {
 
:adapter => 'mysql',
 
:database => DATABASE2,
 
:username => USERNAME,
 
:password => PASSWORD,
 
:host => HOST
 
}
Add connection.rb file in lib folder
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
module Connection
 
  def self.included(base)
 
    base.class_eval do
 
    parameters = self::DB
 
    ActiveRecord::Base.establish_connection(
 
      :adapter  => parameters[:adapter],
 
      :host     => parameters[:host],
 
      :username => parameters[:username],
 
      :password => parameters[:password],
 
      :database => parameters[:database]
 
      )
 
   end
 end
end
include connection file and set DATABASE in models
in Company model
1
2
DB =  DB1
include Connection
in Website model
1
2
3
DB = DB2
 
include Connection


From, http://blog.brijeshshah.com/multiple-database-connections-in-rails/


it's really helpful.

Powered by Blogger.