Hi,
Here is the code of a "simple search form" (thanks to jordinl) which I try to improve. I would like to add the case if there is no match found.
So, there is the view (views/users/index.html.erb)
<% form_tag users_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
<% end %>
<% @users.each do |user| %>
<p><%= link_to "#{user.name}", user %></p>
.
.
.
<% end %>
The controller ( users_controller.rb)
def index
@users = User.search(params[:search])
end
and the method in user model:
def self.search(search)
search.blank? ? [] : all(:conditions => ['name LIKE ?', "%#{search.strip}%"])
end
I tried the following:
def self.search(search)
if search.to_s.size < 1
[]
else
if @users.size > 0
all(:conditions => ['name LIKE ?', "%#{search.strip}%"])
else
render :text => "No result found"
end
end
end
reporting the following error: "You have a nil object when you didn't expect it!..." (no instance in the array). Then, I tried to add
<% if @users? %>
<% @users.each do |user| %>
.
.
.
In the view. But it doesn't work either.
I would be pleased to understand why I'm wrong on this. Thank you!