About ruby on rails, hackintosh, iphone3g and etc.

Monday, May 11, 2009

Rails link_to Previous Page

There is a useful shorthand in Ruby On Rails.

:back

< %= link_to "Back", :back % >

is equivalent to redirect_to(request.env["HTTP_REFERER"])


Saturday, May 9, 2009

Rails Autocomplete Plugin

To enable Rails Autocomplete Plugin, you need the following steps:

1. Install plugin:

script/plugin install auto_complete

2. Add this line to the controller where auto_complete to be used:

auto_complete_for :contact, :name

3. < %= text_field_with_auto_complete :contact, :name, { :size => 10 }, { :skip_style => false,
:method => :get} % >


Using :method => :get overcomes the problem with InvalidAuthenticityToken.

4. To customize the result, you can override a method in your controller.

def auto_complete_for_contact_name
@user = User.find_by_id(session[:user_id])
re = Regexp.new("^#{params[:contact][:name]}", "i")
find_options = {:conditions => ['company_id = ?', "#{@user.company_id}"],:order => "name ASC" }
@organizations = Contact.find(:all, find_options).collect(&:name).select { |org| org.match re }

render :inline => "< %= content_tag(:ul, @organizations.map { |org| content_tag(:li, h(org)) }) % >"
end


The example above shows that the result will be filtered by company_id and sorted by name.

5. To beauty the autocomplete results, the css below can be helpful.

div.auto_complete {
position:absolute;
width:250px;
background-color:white;
border:1px solid #888;
margin:0px;
padding:0px;
}
li.selected { background-color: #ffb; }

Friday, May 8, 2009

Using ISAPI Rewrite on Rails App with IIS and Mongrel as Windows Service

In setting up a multiple production environment in a single server, IIS alone cannot do the redirection of url nicely. When a RAILS app runs in IIS, it requires port number and to redirect domain name to the particular port number, ISAPI Rewrite can help. Below are some sample of how to redirect a hostname to the port required.

RewriteCond %{HTTP:Host} ^(?:www\.)?domain name\.biz$
RewriteRule (.*) http://www.domain name.biz:3000$1 [R=301,L]

RewriteCond %{HTTP:Host} ^(?:www\.)?another domain name\.com$
RewriteRule (.*) http://www.another domain name.com:3001$1 [R=301,L]

Updated: 27th May 2009
Possible to do URL masking by using rewrite proxy

RewriteCond %{HTTP:Host} ^(?:www\.)?another domain name\.com$
RewriteProxy (.*) http://localhost:4002$1

Note: it will map to localhost:4002 and transparent to user as the url shown is www.anotherdomainname.com. You can point multiple domain name to a single web app.

To get this done:
1. Install ISAPI Rewrite.
2. Copy and replace domain name to your own domain name in ISAPI rewrite.
3. If you have more than one domain pointing to the same server, just add below the first domain.

Thursday, April 30, 2009

Calendar Date Select for Ruby on Rails

CalendarDateSelect is semi-light-weight (about ~20k of javascript/css) and easy to use (but gets even lighter with bundle-fu)! It takes full advantage of the prototype.js library, resulting in less code, but maintaining a great deal of functionality.

Some of it's capabilities:

can pick a date, or a date-time, or a date with an optional time, as a popup selector or an embedded calendar!
speaks several different formats
is easy to localize
is extensible. Calendar Date Select doesn't do everything under the sun (which is a good thing), but you can build on top of it.
Installation / Usage

Install the calendar_date_select gem
sudo gem install calendar_date_select
Add a gem dependency to your rails project (in environment.rb)
config.gem "calendar_date_select"
Then, restart/start your rails project. All necessary files will be copied to your public folder.
Now include this at the top of your layout: (you'll also need to include prototype, if you haven't already!)
<%= calendar_date_select_includes "red" # or "blue", or "silver", or nil for the default! %>
# (if you want more control, you can get a list of stylesheets / javascripts via calendar_date_select_javascripts and calendar_date_select_stylesheets)
See the Demo for various uses of CalendarDateSelect.

Wednesday, April 29, 2009

Date Calculation in Rails

>> Date.new( 2008, 1, 1 ).to_time.advance( :months => 1 )
=> Fri Feb 01 00:00:00 +0000 2008


>> Date.new( 2008, 2, 1 ).to_time.advance( :months => 1 )

=> Sat Mar 01 00:00:00 +0000 2008

>> Date.new( 2008, 1, 1 ).to_time + 1.month
=> Thu Jan 31 00:00:00 +0000 2008

Extracted from here:
http://roninonrails.blogspot.com/2008/01/beware-of-rails-date-arithmetic.html

Tuesday, April 28, 2009

Get Month Name in Rails

To get month name as constants, do the following

require 'date'

for i in (1..12)
@month = Date::MONTHNAMES[i]
end

Date Format in strftime

FormatMeaning
%aThe abbreviated weekday name (“Sun'’)
%AThe full weekday name (“Sunday'’)
%bThe abbreviated month name (“Jan'’)
%BThe full month name (“January'’)
%cThe preferred local date and time representation
%dDay of the month (01..31)
%HHour of the day, 24-hour clock (00..23)
%IHour of the day, 12-hour clock (01..12)
%jDay of the year (001..366)
%mMonth of the year (01..12)
%MMinute of the hour (00..59)
%pMeridian indicator (“AM'’ or “PM'’)
%SSecond of the minute (00..60)
%UWeek number of the current year, starting with the first Sunday as the first day of the first week (00..53)
%WWeek number of the current year, starting with the first Monday as the first day of the first week (00..53)
%wDay of the week (Sunday is 0, 0..6)
%xPreferred representation for the date alone, no time
%XPreferred representation for the time alone, no date
%yYear without a century (00..99)
%YYear with century
%ZTime zone name
%%Literal “%'’ character