scud bomb
scud bomb

Reputation: 417

Returning search results inside of a Rails 3 app

We followed Ryan Bates' Railscast #37, and the search form appears without error on our app, but is not functional. It doesnt return any search results.

UsersController:

class UsersController < ApplicationController

def index
  @title = "All users"

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

The search function is defined in our user.rb file

def self.search(search)
  if search
  where('name LIKE ?', "%#{search}%")
  else
  all
 end
end

and finally our index.html.erb file where that search box is displayed

<h1>All users</h1>

<%= form_tag users_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>

Upvotes: 0

Views: 907

Answers (1)

manafire
manafire

Reputation: 6084

Are you looping though somewhere to display the results? You do have matching records in the database, right?

# create some users in the console
User.create([{:name => "Bo Jangles"}, {:name => "Some dude"}])

# somewhere in index.html.erb
<% @users.each do |user| %>
  <p><%= user.name %></p>
<% end %>

Upvotes: 2

Related Questions