Thursday, August 30, 2007

How to load selective fixtures to db in rails

All of you must know how to load fixtures to db

rake db:fixtures:load

To load a subset of your fixtures to your projects db

rake db:fixtures:load FIXTURES=users,stores

Tuesday, August 28, 2007

Star rating in ruby on rails

Recently I needed to implement star rating system for one of my project I am working on.

If you are not familiar with a star rating system, its simply a method of voting using (usually) 5 stars in a row, which will change colour as you hover over them indicating the level at which to rate something. You may think a simple rollover would accomplish this but difficulty arises because as you rollover each star it should stay highlighted while you light up the next one and so on until the end of the row of stars.

click here to find more details>>

Thursday, August 2, 2007

How to Paginate With Ajax

Pagination is a very common task in web application development. But sometimes we need to paginate something with ajax request. The interest to use Ajax for this is to provide a dynamic interface which doesn’t need to reload the entire page when next results(page) displayed. Ajax is really nicely integrated with Rails, and using it is very easier with this great framework. To work with ajax requests your browser must be enabled to handled java script requests. I knew two methods for that problem.

For More details click here >>

Tuesday, July 3, 2007

Varify Email address Format

Useful expressions for email address validation

Matches a limited version of the RFC 2822 addr-spec form.

/\A(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&
\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]
(?!\.)){0,61}[a-zA-Z0-9]?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!$)){0,61}
[a-zA-Z0-9]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]
?\d{1,2}|2[0-4]\d|25[0-5])\]))\Z/i

Friday, June 29, 2007

Multiple Checkboxes with HABTM

Has and Belongs to Many with Multiple Check boxes

So if you are trying to do a multiple select of checkboxes and using habtm in your project, but when you submit the form, only one value was available in your controller. While you try to edit records in database but because of some error you get back to the pre field form and you found that the checkboxes checked by you gone ,then here’s the solution

Model:


class Customer < ActiveRecord::Base
has_and_belongs_to_many :intrests
end

Controller code:

class CustomersController < ApplicationController
def create
if request.post?
@customer=Customer.new(params[:customer])
@customer.save
end
end

def edit
@customer=Customer.find_by_id(params[:id]) if params[:id]
if @customer
if request.post?
if @customer.update_attributes(customer)
flash.now[:message]="Update successfully "
end
end
else
flash[:message]="Page requested by you does not exists"
end
end
end


Your View:

<% form_for :customer, do |f| -%>
First Name:
<%= f.text_field :first_name -%>
Last Name:
<%= f.text_field :last_name -%>
<% for intr in total_intrests -%>
<%= check_box_tag "customer[interest_ids][]","#{intr.id}",
interest(intr) -%> # interest is a helper method
<%= "#{intr.name}" -%>
<% end -%>
<% end -%>

helper method
def interest(i)
if @customer
@customer.interests.include?(i)
else
false
end
end

View generate a checkbox for every interest(all_interest=Interest.find(:all)). The name of the input is significant obviously. The trailing “[]” on the name means the end result will be the list of checked ids. This list will be stored on the @params['customer'] hash with the key ‘interest_ids’. When the controller calls @customer.update_attributes(@params[:customer]), it tries to call @customer.key= for each of the keys on @params[:customer]. What’s important to realize is that these keys don’t have to actually be attributes on the Customer model. All that’s important is that there’s a key= method on the model. Model automatically contains a “collection_ids=” method for habtm and has-many associations.

This method will load the objects identified by the ids and call the “interest=(list)” method on the model with the freshly loaded list. This method in turn, will compare the list to the current list of interests and delete/add interests as necessary.

Thursday, June 28, 2007

How to generate CSV files in Rails

Your controller code :

require 'csv'


def export_to_csv
@customers=CustomerInformation.find(:all)
report = StringIO.new
CSV::Writer.generate(report, ',') do |title|
title << ['Id','Job Title','First Name','Last Name']
@customers.each do |c|
title << [c.id,c.job_title,c.first_name,c.last_name]
end
end

report.rewind
send_data(report.read,:type=>'text/csv;charset=iso-8859-1;
header=present',:filename=>'report.csv',
:disposition =>'attachment', :encoding => 'utf8')
end
Your view:

<% form_tag({ :action => :export_to_csv })do -%>
<%= submit_tag "Export To CSV" -%>
<% end -%>

my another blog

Friday, June 22, 2007

Active Scaffold

Active scaffold is a plug in provide Ajax based user interface for a rails project. You can use it for Admin side activities

* An AJAXified interface for creating, updating and deleting objects
* Automatic handling of ActiveRecord associations
* Sorting and Searching
* CSS styling and theming support
* Work on Firefox 1+, IE 6+ and Safari 2+
* Released under the MIT License, the same one as Rails itself, so you can use it freely in your commercial applications.


Step-1:
Install the plugin from the url:

./script/plugin install http://activescaffold.googlecode.com/svn/tags/active_scaffold

Step-2:
Add to your layout

<%= javascript_include_tag :defaults %>
<%= active_scaffold_includes %>


Step-3:
Add to your controller

active_scaffold :model_name

Active scaffold requires a separate controller for a single model.If you do not want some attributes fields...
Add to your Application controller

ActiveScaffold.set_defaults do |config|
config.ignore_columns.add [:created_at, :updated_at, :status]
end


or you can add to your controller for the attributes fields you want to to display

active_scaffold :TechniquesEmployed do |config|
config.label = "Techniques Employed"
config.columns = [:techniques_employed]
columns[:techniques_employed].label = "Technique Employed"
end

Wednesday, June 13, 2007

Let me Introduce My Self

Myself Satish Chauhan from Faridabad (haryana), India. I am a Electronics Engineer .
I completed my engineering from YMCA Institute of Engineering, Faridabad (Harayana). Presently I am working with Vinayak Solutions Pvt. Ltd.(Vinsol) as a Software Engineer .

Vinsol is a software development(Web development) company based in New Delhi , India. It works on ruby on rails.
I am enjoying Ruby and Ruby On Rails