Ajax pagination links: Create pagination links with link_to_remote

Here is a method by mixonix to create pagination links by link_to_remote. Copy this code in your helpers/application_helper.rb

def ajax_pagination_links(paginator, options={})
options.merge!(ActionView::Helpers::PaginationHelper::DEFAULT_OPTIONS) {|key, old, new| old}
window_pages = paginator.current.window(options[:window_size]).pages
return if window_pages.length <= 1 unless
options[:link_to_current_page]

first, last = paginator.first, paginator.last

returning html = ” do
if options[:always_show_anchors] and not window_pages[0].first?
html << link_to_remote(first.number, :update => options[:update], :url => { options[:name] => first }.update(options[:params] ))
html << ' ... ' if window_pages[0].number - first.number > 1
html << ' '
end

window_pages.each do |page|
if paginator.current == page && !options[:link_to_current_page]
html << page.number.to_s
else
html << link_to_remote(page.number, :update => options[:update], :url => { options[:name] => page }.update(options[:params] ))
end
html << ' '
end

if options[:always_show_anchors] && !window_pages.last.last?
html << ' ... ' if last.number - window_pages[-1].number > 1
html << link_to_remote(paginator.last.number, :update => options[:update], :url => { options[:name] => last }.update( options[:params]))
end
end
end and use following code for creating links

<%= ajax_pagination_links @pages, {:params => {:search_query => @params[:search_query]} } %>

Tab Problem in rails .rhtml files

The situation was: running Ruby 1.8.4, Rails 1.1.2, on WinXP & WEBrick server.

My application was working fine, but as I installed Rmagick my application crashed.
I got strange errors like:

compile error /script/../config/../app/views/layouts/application.rhtml:18: parse error, unexpected $, expecting kEND

if I refresh again the error actually changes(further refreshes flip back & forth between errors):

compile error

/script/../config/../app/views/layouts/application.rhtml:18: Invalid char `01′ in expression

./script/../config/../app/views/layouts/application.rhtml:19: parse error, unexpected tCONSTANT, expecting kEND

./script/../config/../app/views/layouts/application.rhtml:20: parse error, unexpected tCONSTANT, expecting kEND

./script/../config/../app/views/layouts/application.rhtml:21: Invalid char `06′ in expression

./script/../config/../app/views/layouts/application.rhtml:21: parse error, unexpected $, expecting kEND

Then my colleagues told me the root of this problem, this was because of tabs in .rhtml files. Also they told me the simple solution:

Put template = template.gsub(/\t/, ” “) in your

\vendor\rails\actionpack\lib\action_view\base.rb file at line 496 as very first line of def compile_template
Restart webserver and you are done….

converting all newline characters to br tag

I was surprised as there is no function in ruby to convert all newline characters to <br>.

Here is a php nl2br equivalent method to convert all newline characters (\n) to break tag (<br>) in a string.

 def nl2br(s)
s.gsub(/\\n/, '<br>')
end

Plugin: validate_request

A very useful plugin by Scott A. Woods.

validate_request plugin allows us to check the request method and parameters that are used to call your action.

For Example consider an add_to_cart action as:


def add_to_cart
@product = Product.find(params[:id])
@cart.add_product(@product)
end

The link to add an item to our cart should like store/add_to_cart/nnn, where nnn is an integer. There will be an error if some one intentionaly enter store/add_to_cart/some_string_here.
ValidateRequest allows us to double check these things, and act appropriately. For instance, we could solve the above problem by adding one line to our action:


def add_to_cart
validate_request(:get, :id => :integer) or return
@product = Product.find(params[:id])
@cart.add_product(@product)
end

The validate_request method will now verify that incoming requests are via the GET method, and that they contain one argument called ‘id’ whose value is an integer. If any of these conditions aren’t true, the requester is redirected to the site’s home page (configurable, of course), and flash[:error] is set with a polite message (also configurable).

Install the plugin by running the following commands from your rails application’s directory:

./script/plugin source svn://rubyforge.org//var/svn/validaterequest/plugins ./script/plugin install validate_request

For more details visit http://rubyforge.org/projects/validaterequest/

Plugin: tabnav

Add tabbed navigation to your Rails application in a easiest way.

For more details please visit http://blog.seesaw.it/articles/2006/07/23/the-easiest-way-to-add-tabbed-navigation-to-your-rails-app

An Escalator Puzzle

Bob takes the underground train to work and uses an escalator at the railway station. If Bob runs up 6 steps of the escalator, then it takes him 52.5 seconds to reach the top of the escalator. If he runs up 14 steps of the escalator, then it takes him only 32.5 seconds to reach the top.

How many seconds would it take Bob to reach the top if he did not run up any steps of the escalator at all?

JotSpot.com

JotSpot Live allows you, your colleagues or clients to take notes together on the same web page at the same time. Imagine everyone simultaneously typing and editing the same Microsoft Word document and you’ll get the idea. Visit JotSpot.com

19 Rails Tricks Most Rails Coders Don’t Know

I was searching for some Rails tips and found a very good post, Click here to visit

Configuring Multiple Rails Application with Lighttpd

I was using lighttpd web server for my Rails applications.
In development mode I was starting lighttpd server from the rails application directory by

lighttpd -D -f config/lighttpd.conf
But I was facing problem when I tried to run multiple rails applications in production mode. After some trials I succeded.

I inserted the following lines of code in my /etc/lighttpd/lighttpd.conf file

$HTTP["host"]== “domain.com” {

server.error-handler-404 = “/dispatch.fcgi”

server.document-root = “/home/railsapp/public/”

server.errorlog = “/home/railsapp/log/lighttpd.error.log”

accesslog.filename = “/home/railsapp/log/lighttpd.access.log”

url.rewrite = ( “^/$” => “index.html”, “^([^.]+)$” => “$1.html” )

fastcgi.server = ( “.fcgi” => ( “localhost” => (

“min-procs” => 1,

“max-procs” => 1,

“socket” => “/home/railsapp/tmp/sockets/fcgi.socket”,

“bin-path” => “/home/railsapp/public/dispatch.fcgi”,

“bin-environment” => ( “RAILS_ENV” => “production” )

) ) )

}

$HTTP["host"]== “another.domain.com” {

server.error-handler-404=”/dispatch.fcgi”

server.document-root = “/home/anotherrailsapp/sparitual/public/”

server.errorlog = “/home/anotherrailsapp/log/lighttpd.error.log”

accesslog.filename = “/home/anotherrailsapp/log/lighttpd.access.log”

url.rewrite = ( “^/$” => “index.html”, “^([^.]+)$” => “$1.html” )

compress.filetype = ( “text/plain”, “text/html”, “text/css”, “text/javascript” )

compress.cache-dir = “/home/anotherrailsapp/tmp/cache”

fastcgi.server = ( “.fcgi” => ( “localhost” => (

“min-procs” => 1,

“max-procs” => 1,

“socket” => “/home/anotherrailsapp/tmp/sockets/fcgi.socket”,

“bin-path” => “/home/anotherrailsapp/public/dispatch.fcgi”,

“bin-environment” => ( “RAILS_ENV” => “production” )

) ) )

}

Then started the lighttpd server by

lighttpd -D -f /etc/lighttpd/lighttpd.conf

And it worked…

Cartographer: A Google maps API in Rails

If you want to insert google maps in your rails application, then you can use cartographer.
This is still in development.