Category: Ruby on Rails

No Such File to load — readline (LoadError) in ruby script/console

By , December 7, 2010 4:01 am

If you get following error while extecuting  ruby script/console


/usr/local/lib/ruby/1.8/irb/completion.rb:10:in `require’: no such file to load — readline (LoadError)
 from /usr/local/lib/ruby/1.8/irb/completion.rb:10
 from /usr/local/lib/ruby/1.8/irb/init.rb:252:in `require’
 from /usr/local/lib/ruby/1.8/irb/init.rb:252:in `load_modules’
 from /usr/local/lib/ruby/1.8/irb/init.rb:250:in `each’
 from /usr/local/lib/ruby/1.8/irb/init.rb:250:in `load_modules’
 from /usr/local/lib/ruby/1.8/irb/init.rb:21:in `setup’
 from /usr/local/lib/ruby/1.8/irb.rb:54:in `start’
 from /usr/local/bin/irb:13

To solve this issue , you need to install some libraries.


sudo apt-get install libncurses5-dev
sudo apt-get install libreadline5-dev

Then goto ruby source folder and ext/readline folder


cd  <RUBY SOURCE FOLDER>/ext/readline
ruby extconf.rb
make
sudo make install

This will solved your readline error. If you still facing any problem let me know.

Useful Date methods

By , November 12, 2010 4:53 am

Here I list down some of useful date methods. Generally following functions are used in Reports where reports required between specific period of time.

For example “This Month” data report  required starting date and ending date of month.

So by following way, both dates can get.


time = Time.now.to_date

Fri, 12 Nov 2010

from = time.beginning_of_month

Mon, 01 Nov 2010

to = time.end_of_month

Tue, 30 Nov 2010

For  ”This Year”

from = time.beginning_of_year

Fri, 01 Jan 2010

to = time.end_of_year

Fri, 31 Dec 2010

For “Today”

time.end_of_year

Fri, 31 Dec 2010

time = Time.now

Fri Nov 12 17:05:08 +0530 2010

time.beginning_of_day

Fri Nov 12 00:00:00 +0530 2010

time.end_of_day

Fri Nov 12 23:59:59 +0530 2010

MySql Ruby gem Installation

By , October 25, 2010 2:37 am

T5DXZA3B5J78

To build and install mysql ruby gem , following simple command is used.


sudo gem install mysql

But sometime, such simple command does not work to install mysql gem when it raised the following error.


/usr/bin/ruby extconf.rb

checking for mysql_ssl_set()... no

checking for rb_str_set_len()... no

checking for rb_thread_start_timer()... no

checking for mysql.h... no

checking for mysql/mysql.h... no

*** extconf.rb failed ***

Could not create Makefile due to some reason, probably lack of

necessary libraries and/or headers.  Check the mkmf.log file for more

details.  You may need configuration options.

I found out number of solutions for this issue. In general one of the following solution worked.


sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql

OR


gem install mysql -- --with-mysql-config=/usr/bin/mysql_config

OR


sudo yum install mysql-devel gcc make

sudo gem install mysql -- --with-mysql-config=/usr/bin/mysql_config

I hope this help to install mysql ruby gem. If you know any other configuration require to install mysql then please share it.

AJAX Pagination

By , September 10, 2010 12:19 am

Earlier I writtern a blog about AJAX pagination in rails in which changes required in will_paginate files.

But now no change is required in will_paginates file(s) for ajax pagination.

For ajax pagination , you need to add following js code in application.js file.

Note: Add spinner.gif image in images folder .


//For AJAX Pagination:

document.observe("dom:loaded", function() {
// the element in which we will observe all clicks and capture
// ones originating from pagination links
var container = $(document.body)

if (container) {
var img = new Image
img.src = '/images/spinner.gif'

function createSpinner() {
return new Element('img', { src: img.src, 'class': 'spinner' })
}

container.observe('click', function(e) {
var el = e.element()
if (el.match('.pagination.ajax a')) {
el.up('.pagination.ajax').insert(createSpinner())

new Ajax.Request(el.href, { method: 'get' })
e.stop()
}
})
}
})

Call AJAX pagination: Add class pagination ajax  in method


<%=will_paginate @object,:class=>'pagination ajax'%>

Call  Non AJAX pagination: Simply call will_paginate method


<%=will_paginate @object%>

Set your controller method according you ajax / non-ajax call

For ajax call , you need to update a div ,


@object = Model.paginate :page=>params[:page],:per_page => 10

render  :update do |page|

page.replace_html :div_id ,:partial => 'result_page'

end

I hope this will help make simple ajax pagination.

Filename rename in File Column

By , September 9, 2010 12:56 am

File column is very useful plugin to upload multi sizes of images. Currently in file column plugin we are not able to provide file name.

I found number of forums which are facing this issue but not able to find the answer.

I found the solution which required to customize the file column plugin.

Add option filename in file column method.


T= Time.now
file_column :uploaded_data, :magick => {
:versions => { "thumb" => "150x150", "medium" => "640x480>" }
},:filename => "#{T.strftime("%Y%m%d%H%M")}"

Initialized filename option in FileColumn.rb file


def self.init_options(defaults, model, attr)
options = defaults.dup
options[:store_dir] ||= File.join(options[:root_path], model, attr)
options[:filename]||=nil
unless options[:store_dir].is_a?(Symbol)
options[:tmp_base_dir] ||= File.join(options[:store_dir], "tmp")
end
options[:base_url] ||= options[:web_root] + File.join(model, attr)

[:store_dir, :tmp_base_dir].each do |dir_sym|
if options[dir_sym].is_a?(String) and !File.exists?(options[dir_sym])
FileUtils.mkpath(options[dir_sym])
end
end

options
end

Assign filename to temp image path. Update code @class TempUploadedFile store_upload method line number 219


@filename = options[:filename] || FileColumn::sanitize_filename(file.original_filename)

Now test your code and get the uploaded image new file name.

Let me know if you face any issue.

Panorama Theme by Themocracy