SitePoint Sponsor

User Tag List

Results 1 to 15 of 15

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    May 2012
    Location
    Karkala, India
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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/%...h.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

  2. #2
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,108
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    Hi Prajwal,

    I first cleaned up your schema with some migrations
    Code Ruby:
    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.
    Code Ruby:
    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.

    Code Ruby:
    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
    Code Ruby:
    Telephonedirectory::Application.routes.draw do
     
      resources :phone_records do
        collection do
          get 'search'
        end
      end
     
      root :to => 'PhoneRecords#index', :as => 'listing'
    end
    Code erb:
    <%= 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
    Code Ruby:
    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,

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2012
    Location
    Karkala, India
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank a ton for this much

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2012
    Location
    Karkala, India
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i have understood all the logic accept in migrations i am not getting it clearly the idea.

  5. #5
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,108
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    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.

    Code Ruby:
    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.

  6. #6
    SitePoint Enthusiast
    Join Date
    May 2012
    Location
    Karkala, India
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  7. #7
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,108
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    You can always download bootstrap manually and add to the assets directory.

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

  8. #8
    SitePoint Enthusiast
    Join Date
    May 2012
    Location
    Karkala, India
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  9. #9
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,108
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    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


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •