views:

41

answers:

1

Could use some help setting up an AJAX Search form for Users...

config.rb - controller:

resources :users, :only => [:index, :show, :searchresult] do
  collection do
      get 'searchresult'
  end
end

Model

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

Controller:

  def searchresult
     @users = User.search(params[:name])    
  end

View:

<% form_tag users_searchresult_path, :method => 'get' do %>

Right now I'm getting the following error: undefined local variable or method `users_searchresult_path' for #<#:0x1092ecdb8>

What do you think? Thanks!

+1  A: 

Run rake routes from your application directory. I think you'll find that the correct path is actually searchresult_users_path.

As a way of explanation: Rails prepends the action, not appends. Think how users#new becomes new_user_path and users#edit becomes edit_user_path. Same with this; users#searchresult becomes searchresult_users_path.

Sidenote: rake routes is one handy tool for looking things like this up. I know it's certainly made my life easier.

vonconrad