Hi Prajwal,
I first cleaned up your schema with some migrations
class CreateCities < ActiveRecord::Migration
def up
create_table :cities do |t|
t.string :name
end
create_table :phone_records do |t|
t.integer :city_id
t.string :phone_no
t.string :name
t.string :address
end
end
end
class MapCities < ActiveRecord::Migration
def up
TelephoneRecord.all.each do |tel|
phone_record = PhoneRecord.new({
:name => tel.name,
:phone_no => tel.phone_no,
:address => tel.address
})
phone_record.city = City.find_or_create_by_name(tel.selectcity)
phone_record.save
end
drop_table :telephone_records
end
end
And added the relationships to the models. The filenames, table names and object names are all important with Rails.
Tables must be plural e.g. phone_records
Everything else about a model needs to be singular, city.rb and the model named City.
class City < ActiveRecord::Base
has_many :phone_records
attr_accessible :name
end
class PhoneRecord < ActiveRecord::Base
belongs_to :city
attr_accessible :name, :phone_no, :address
end
Search parameters GET and POST are combined in the params hash so you can access variables from forms.
class PhoneRecordsController < ApplicationController
def index
@phone_records = PhoneRecord.limit(100).all
end
def search
@phone_records = PhoneRecord.where(:city_id => params[:city_id]).limit(100)
if params[:search_by] == 'name'
@phone_records = @phone_records.where("name LIKE ?", "#{params[:search]}%")
elsif params[:search_by] == 'address'
@phone_records = @phone_records.where("address LIKE ?", "#{params[:search]}%")
elsif params[:search_by] == 'phone'
@phone_records = @phone_records.where("phone_no LIKE ?", "#{params[:search]}%")
end
render :action => :index
end
end
Use explicit paths in your routes
Telephonedirectory::Application.routes.draw do
resources :phone_records do
collection do
get 'search'
end
end
root :to => 'PhoneRecords#index', :as => 'listing'
end
<%= form_tag(search_phone_records_path, :method => "get") do %>
<div>
<%= label_tag(:city, "City:") %>
<%= select_tag(:city_id, options_from_collection_for_select(City.all, :id, :name)) %>
</div>
<div>
<label>Search By:</label>
<%= radio_button_tag(:search_by, "name") %>
<%= label_tag(:search_by_name, "Name") %>
<%= radio_button_tag(:search_by, "address") %>
<%= label_tag(:search_by_address, "Address") %>
<%= radio_button_tag(:search_by, "phone") %>
<%= label_tag(:search_by_phone, "PhoneNo") %>
</div>
<div>
<label>Search:</label>
<%= text_field_tag(:search, params[:search]) %>
</div>
<div>
<%= label_tag(:match, "Match:") %>
<%= select_tag(:match, options_for_select([['Starts with', 'starts_with']])) %>
</div>
<div>
<%= submit_tag("Search") %>
</div>
<% end %>
And the controller code
class PhoneRecordsController < ApplicationController
def index
@phone_records = PhoneRecord.limit(100).all
end
def search
@phone_records = PhoneRecord.where(:city_id => params[:city_id]).limit(100)
if params[:search_by] == 'name'
@phone_records = @phone_records.where("name LIKE ?", "#{params[:search]}%")
elsif params[:search_by] == 'address'
@phone_records = @phone_records.where("address LIKE ?", "#{params[:search]}%")
elsif params[:search_by] == 'phone'
@phone_records = @phone_records.where("phone_no LIKE ?", "#{params[:search]}%")
end
render :action => :index
end
end
At the moment the “match” parameter isn’t doing anything and all search are “starts_with” but if you wanted to implement other types you can give it a go.
Hope it helps,