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
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
Posted by Satish Chauhan at 6:12 PM 1 comments
Labels: fixtures, rails, ror, 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>>
Posted by Satish Chauhan at 12:34 AM 1 comments
Labels: rails, rating, ror, ruby on rails
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 >>
Posted by Satish Chauhan at 11:00 AM 1 comments
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
Posted by Satish Chauhan at 4:18 PM 32 comments
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
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
Posted by Satish Chauhan at 1:07 PM 39 comments
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
Posted by Satish Chauhan at 9:51 AM 26 comments
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
Posted by Satish Chauhan at 11:48 AM 3 comments
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
Posted by Satish Chauhan at 11:15 PM 0 comments