activerecord - Rails 4 search bar -
i'm trying create search bar in rails 4 app. i'm user db has 'name' , 'email' columns user's - want users able search other users name or id.
i'm getting this:
activerecord::recordnotfound in userscontroller#index couldn't find users 'id': (all, {:conditions=>["name ?", "%hi@example.com%"]}) (found 0 results, looking 2)
does know i'm doing wrong? i've looked @ railscasts , few forums etc cant past point @ moment.
index.html.erb:
<% form_tag users_path, :method => 'get' %> <p> <%= text_field_tag :search, params[:search] %> <%= submit_tag "search", :name => nil %> </p> <% end %>
model/user.rb:
def self.search(search) if search find(:all, :conditions => ['name ?', "%#{search}%"]) else find(:all) end end
users_controller.rb:
def index @users = user.search(params[:search]) end
routes.rb:
'search' => 'users#index'
are using rails 4? find(:all, ...)
old way of doing things. find takes ids now. use:
def self.search(search) if search.present? where('name ?', "%#{search}%") else where(true) end end
also present?
test against both nil , blank. maintain in mind can slow depending on database.
activerecord ruby-on-rails-4
No comments:
Post a Comment