Friday, November 24, 2006

This blog is moved to http://webonrails.com

This blog is moved to http://webonrails.com

Thursday, September 14, 2006

Lib file state_select.rb

If You you are not interested in installing a plugin just for one method(state_select) then You can put state_select.rb file in you rails lib directory. And include state_select.rb file in your controller where ever you want to use state_select method.


The url for state_select.rb is http://opensvn.csie.org/state_select/trunk/lib/state_select.rb

Feedback please....

Plugin: state_select, generate drop down selection box for states

I have wrote my first plugin(state_select). This plugin allows to create drop down list for states, same as country_select method in rails. I know this is not a big deal...

Curently it can generate state list for India, US, and Canada(default is US).

Usage:


  • state_select(object, method, country='US', options = {}, html_options = {})
    Return select and option tags for the given object and method, using state_options_for_select to generate the list of option tags.

  • state_options_for_select(selected = nil, country = 'US')
    Returns a string of option tags for states in a country. Supply a state name as selected to have it marked as the selected option tag.
NOTE: Only the option tags are returned, you have to wrap this call in a regular HTML select tag.

The svn repository can be found at http://opensvn.csie.org/state_select/trunk/

Install plugin by
script/plugin install http://opensvn.csie.org/state_select/trunk/

Deprecated Finders in Rails 1.1.5

These Finders are deprecated



  • find_first [use find(:first)]

  • find_all [use find(:all)]

  • find_on_conditions [use find(:conditions)]

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

Friday, August 04, 2006

Make ids in URL Search engine friendly

Generally in rails url are in form of :controller/:action:/:id for example post/view/9 .

URLs are considered extremely valuable. Not only because users have to see them all the time, but also because search engines give them a lot of weight: since it’s a “limited resource” where you can only include a few keywords, you better use the keywords that matter most.

We can use both post id and post title in the url to make them more search engine friendly as post/view/9-this-is-the-post-title.

This is simple, as rails treats :id as a special parameter in routes. It’s specialness comes from the fact that it would try to call the to_param method on any object passed when creating URLs. That’s why url_for :id => @post is equivalent to url_for :id => @post.id because ActiveRecord model’s have a default to_param that returns the id of the object.

All you need to do is define your own to_param for your models, and make sure you don’t explicitly include the .id in your url_for and link_to, because then you would be skipping your own to_param call.


class Post < ActiveRecord::Base
def to_param
"#{id}-#{full_name.gsub(/[^a-z1-9]+/i, '-')}"
end
end

You can change -(hyphen) in gsub in to_param method by _, + or anything you wish.

Thursday, July 13, 2006

19 Rails Tricks Most Rails Coders Don’t Know

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

Wednesday, June 28, 2006

Configuring Multiple Rails Application with Lighttpd

I was using lighttpd web server for my Rails applications.
In development mode I was starting lighttpd server by

lighttpd -D -f config/lighttpd.conf

But I was facing problem when I tried to run multiple rails applications. 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...