How to build a search form in this case and display results

i have a to create form some thing like this and display results from database for the values in form

http://www.chhattisgarh.bsnl.co.in/%28S%28izqqxpzvdfsn434525viqujd%29%29/directory_services/AreaWiseSearch.aspx?Area=04

i have created and stored database with table name telephone_records consisting of selectcity,match,phone_no ,name, address.

how to create a form retrieves data from database and display phone-no,name,address in table by selecting values from above form

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,

thank a ton for this much

i have understood all the logic accept in migrations i am not getting it clearly the idea.

Migrations are simply scripts that performs changes to your database schema.
Each file can have an up and down method to migrate the db schema backwards or forwards.

I create two new tables in your database cities and phone_records.
I only create the new phone_records so that it had a proper id column and used the rails conventions.

The second migration is just moving the rows from your previous table to the new one.


phone_record.city = City.find_or_create_by_name(tel.selectcity)

The find_or_create_by helper creates the city if it doesn’t exist or returns it.

To perform migrations you use rake db:migrate to move forward and rake db:rollback to move back a step.

i am trying to design the form as well as table displaying results using twitter bootstrap the problem is when i try to install twitter bootstrap rails gem it says libv8 buiding native extension then error message i am on windows xp sp2 and found that it is not suppoorted so which is the alternate method to do it using bootstrap itself i tried a gem bootstrap sass but when i try along it with formtastic-bootstrap i get an error once again could you let me know there r any other gem using twitter bootstrap and also deals with form_tag command please let me know.

You can always download bootstrap manually and add to the assets directory.

What’s the error you are getting when installing formtastic-bootstrap ?

done mark installed it finally ssl connect error i was getting done but ican this form_tag can be used with twitter bootstrap everywhere i am seeing it form_for being used.

To apply the bootstrap styles you just need to make the HTML the same as what it expects.
http://twitter.github.com/bootstrap/base-css.html#forms

load error: bootstrap formastic you said to download it manually in assets folder how to link it with given form and table then i know which commands to get things done manually downloaded it how to link it to app.

table design done as it was with html now mark tell me should i change this form in html to use bootstrap form_tag i am unable to get it at the moment.

If you can’t get the bootstrap formtastic gem don’t worry about it.

All it does is generate HTML that bootstrap needs, all you need to do is render the same HTML and classes that the forms need.

i used bootstrap manually extracted it to assets in app then into js and stylesheets finally was able to get a table design but at the moment i am trying to apply to form_tag but i am not getting where actually classes in form_tag must be defined should i rebuild the entire form into html once again or is it possible to use bootstrap for it directly i searched for solution but everything is with form_for and bootstrap i never found bootstrap dealing with such form_tag command is there a way to el with form_tag and bootstrap

got it done …

even tough this looks simple problem i want to display the results after submitting search button in same page and for also various other search results here record are displayed first itself and then for then table is changed according to search results presently trying a solution for it any idea mark should i change some in controller or view bit of a trouble.