0

I need a search form in my rails 3.2.3 application, but i don't know how to do this, yesterday i had it fixed but then i deleted it :(.

Here are my Controller and Model:

Controller

class BedrijfsgegevensController < ApplicationController   
  def index
    @bedrijfsgegevens = Bedrijfsgegeven.all
    @bedrijfsgegevens = Bedrijfsgegeven.search(params[:search])
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @bedrijfsgegevens }
    end
  end

  # GET /bedrijfsgegevens/1
  # GET /bedrijfsgegevens/1.json
  def show
    @bedrijfsgegeven = Bedrijfsgegeven.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @bedrijfsgegeven }
    end
  end

  # GET /bedrijfsgegevens/new
  # GET /bedrijfsgegevens/new.json
  def new
    @bedrijfsgegeven = Bedrijfsgegeven.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @bedrijfsgegeven }
    end
  end

  # GET /bedrijfsgegevens/1/edit
  def edit
    @bedrijfsgegeven = Bedrijfsgegeven.find(params[:id])
  end

  # POST /bedrijfsgegevens
  # POST /bedrijfsgegevens.json
  def create
    @bedrijfsgegeven = Bedrijfsgegeven.new(params[:bedrijfsgegeven])

    respond_to do |format|
      if @bedrijfsgegeven.save
        format.html { redirect_to @bedrijfsgegeven, notice: 'Bedrijfsgegeven was successfully created.' }
        format.json { render json: @bedrijfsgegeven, status: :created, location: @bedrijfsgegeven }
      else
        format.html { render action: "new" }
        format.json { render json: @bedrijfsgegeven.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /bedrijfsgegevens/1
  # PUT /bedrijfsgegevens/1.json
  def update
    @bedrijfsgegeven = Bedrijfsgegeven.find(params[:id])

    respond_to do |format|
      if @bedrijfsgegeven.update_attributes(params[:bedrijfsgegeven])
        format.html { redirect_to @bedrijfsgegeven, notice: 'Bedrijfsgegeven was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @bedrijfsgegeven.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /bedrijfsgegevens/1
  # DELETE /bedrijfsgegevens/1.json
  def destroy
    @bedrijfsgegeven = Bedrijfsgegeven.find(params[:id])
    @bedrijfsgegeven.destroy

    respond_to do |format|
      format.html { redirect_to bedrijfsgegevens_url }
      format.json { head :no_content }
            end
        end
    end

Model

class Bedrijfsgegeven < ActiveRecord::Base

    def search
    @search = Bedrijfsgegeven.search() do
    keywords(params[:search])
    end
    end

    def self.search(search)
        if search
        find(:all, :conditions => ['Voornaam LIKE ?', "%#{search}%"])
    else
        find(:all)
    end
  end

    validates :voornaam, 
              :achternaam, 
              :woonplaats, 
              :telefoon, 
              :website, 
              :email, 
              :presence => true

  attr_accessible :achternaam, :email, :telefoon, :voornaam, :website, :woonplaats
end

i hope someone could help me out with this.

Grtz Kees

Leopd
  • 41,333
  • 31
  • 129
  • 167
Kees Sonnema
  • 5,759
  • 6
  • 51
  • 106

2 Answers2

1

Have you read / watched the following railscast:

http://asciicasts.com/episodes/240-search-sort-paginate-with-ajax

Helps me every time I need basic search

simonmorley
  • 2,810
  • 4
  • 30
  • 61
  • Likewise, Railscasts is a fantastic place to start though. Don't forget to mark my answer as 'answered' if it helps. – simonmorley May 08 '12 at 09:10
  • I have another question. Now i can search for only 1 value. in my model: def self.search(search) if search find(:all, :conditions => ['voornaam LIKE ?', "%#{search}%"]) else find(:all) end end The only thing i can search for is voornaam (firstname in english) how do i change that so i can search for al the records like voornaam, achternaam, woonplaats, telefoon, website, email? thanks – Kees Sonnema May 08 '12 at 09:24
  • Personally, I use thinking sphinx for advanced search. Faster and pretty easy to set up. You can read about it here: http://freelancing-god.github.com/ts/en/ – simonmorley May 08 '12 at 09:46
  • i Can't work with thinking sphinx because i'm using a Sql Server 2005 database. Sphinx only supports MYSQL and postgreSQL – Kees Sonnema May 08 '12 at 09:52
  • Ok. Yuk ;) Not so familiar with how to deal with that as I jumped from basic search as above, to sphinx. – simonmorley May 08 '12 at 09:54
  • the most tutorials on the internet about useful search engines are very difficult to understand when your in the beginning of working with rails. I hope i can fix it with a nice and advanced search engine. anyways thanks for your help :) – Kees Sonnema May 08 '12 at 09:57
  • No problem. If I find something suitable on my travels I'll send your way. – simonmorley May 08 '12 at 10:02
1

I will recommend you to start using repositories which will make you safe from these incidental delete. You can get 5 free repositries at bitbucket.org. Both git and mercurial are awesome.

Hemant Verma
  • 90
  • 2
  • 9