Topic: Display only filtered Objects in index
Hi, I'm new here and started working with rails only a month ago.
I'm trying to develop a VideoGame Database that is supposed to contain many many entries.
Here's my problem. Currently any new gameentry is listed in my index-page like this
<% @games.each do |game| %>
<tr>
<td><%= game.title_german %></td>
<td><%= game.title_original %></td>
<td><%= game.release %></td>
<td><%= game.dlc %></td>
<td><%= link_to 'Show', game %></td>
<!-- <td><%= link_to 'Edit', edit_game_path(game) %></td> -->
<td><%= link_to 'Destroy', game, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>of course that doesn't make a lot of sense. No Shop or library (like imdb) would have an accessible page listing the intire datatable. So I want the index page only to display those games that were filtered by the searchfield, which looks like this.
<%= form_tag games_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :title_german => nil %>
</p>
<% end %>My Model and my controller are defined as follows:
class Game < ActiveRecord::Base
def self.search(search)
if search
find(:all, :conditions => ['title_german LIKE ?', "%#{search}%"])
else
find(:all)
end
end
end
class GamesController < ApplicationController
def index
@games = Game.search(params[:search])
end
endI was thinking about defining a helper method called "used_search?" in the application_controller to used this method in an if-statement which determines wether to display the index.html.erb or not...would that be the usual way to solve this problem or what do experienced developers do? I have no clue!
I would appreciate any kind of help! thanks