Filter Index Page

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
end

I 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

On Jan 29, 2013, at 10:56 AM, Ryo S. wrote:

<% @games.each do |game| %>
<% end %>

class GamesController < ApplicationController
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

I would look at Kaminari or another “paging” system, and page the
results. Show the first 25 if no search is given (plus navigation to
page through 25 more at a time). That’s as easy as chaining one more
modifier in your controller:

…your_selector.page(params[:page])

You can choose to page your search results along with your regular
results – that’s another implementation detail you can use or not.
Depends on how focused the results are, if they give you back hundreds
of results, you might want to page them as well. Just be sure to use GET
for your search form, and Kaminari will interweave the search
querystring and the page querystring without any extra effort on your
part.

Walter

Thanks for responding?

I watched a Kaminari Tutorial video on railscast and like what it does,
I sure will use it too when I want my search results to be displayed in
small pieces, but it is actually not what I was looking for.

I’m not trying to have my results seperated in pages, but I want the
entire “product”-list NOT to be displayed at all.
The Index page should ONLY display filtered titles, but so far as
default (when opening the index page) EVERY product is listed.

I don’t see Amazon oder IMDB having a page where every single product or
Movie is listed…

def self.search(params)
self.where(params[‘search’]).page(params[:page])
end

I tried Walters suggestion and put my display-table in a partial; this
is what the method looked like

def index
if(params[:q])
@games = Game.search(params[:search])
else
render :partial => ‘search_results’
end
end

But it would cause me a NoMethod Error… it says that the “each”
function was not defined, but why?

Rolando G.s suggestion is simply not changing anything, maybe I’m not
implementing it correctly…

Okay, then you should set that up in your index method. Decide what you
do want to show (maybe a partial with “How to Search” instructions) and
show that instead of ModelName.all.

def index
if(params[:q])
#do your search thing
else
render :partial => ‘search/instructions’
end
end

Walter

On Jan 29, 2013, at 1:58 PM, Ryo S. wrote:

But it would cause me a NoMethod Error… it says that the “each”
function was not defined, but why?

I think you have it backwards. If there’s no query, there’s nothing to
put in the partial, but the partial is needed in the case that you DO
have a search query. Make a second partial that only has “No search yet”
in it. Put that in the else side. In the if side, put this:

render :partial => ‘search_results’, :locals => @games

Inside your partial, change any instance of @games to games (remove the
@).

There’s an even easier way to do this, and that’s by making a single row
of your table as a partial, and saving it with the name _game.html.erb.
Then in your index page, add this:

<%= render @games %>

If you haven’t done so yet, I really recommend a thorough read of the
Rails Guide on Views and Rendering. http://guides.rubyonrails.org

Walter

So this is what the method in the controller looks now

def index
if(params[:q])
render :partial => ‘search_results’, :locals => @games
else
render :partial => ‘no_search_yet’
end
end

and I removed the one “@” in the index view, but it only results in,
whenever I use the search, the “_no_search_yet” partial is being
displayed. So the index page always displays this partial, no matter I
use the search button or not.

I think the problem is, the default link “http://localhost:3000/games
always leads to my index.view (by resource “games”).
Having used the search function end in another Url, but the default
always keeps the full list :confused:

On 30 January 2013 11:33, Ryo S. [email protected] wrote:

and I removed the one “@” in the index view, but it only results in,
whenever I use the search, the “_no_search_yet” partial is being
displayed. So the index page always displays this partial, no matter I
use the search button or not.

I think the problem is, the default link “http://localhost:3000/games
always leads to my index.view (altough not defined in the routes.rb).
Having used the search function end in another Url, but the default
always keeps the full list :confused:

Have a look at the Rails Guide on Debugging. It will show you
techniques that you can use to debug the code.

Colin

in my console it says:

"
Started GET “/games?utf8=%E2%9C%93&search=mega&commit=Search” for
127.0.0.1 at 2013-01-30 15:52:18 +0100
Processing by GamesController#index as HTML
Parameters: {“utf8”=>“V”, “search”=>“mega”, “commit”=>“Search”}
Rendered games/_no_search_yet.html.erb (0.0ms)
Completed 200 OK in 5ms (Views: 0.0ms | ActiveRecord: 0.0ms)"

what does the “:q” actually mean? I didn’t name my search field, as far
as I know, it looks like this

<%= form_tag games_path, :method => ‘get’ do %>


<%= text_field_tag :search, params[:search] %>
<%= submit_tag “Search”, :title_german => nil %>


<% end %>

On Jan 30, 2013, at 6:33 AM, Ryo S. wrote:

and I removed the one “@” in the index view, but it only results in,
whenever I use the search, the “_no_search_yet” partial is being
displayed. So the index page always displays this partial, no matter I
use the search button or not.

What do you see in the console while you test? Did you name your search
field q? params[:q] won’t be filled unless that’s the case. I can’t
recall whether you did or not.

Walter

On Jan 30, 2013, at 9:54 AM, Ryo S. wrote:

what does the “:q” actually mean? I didn’t name my search field, as far
as I know, it looks like this

Yes you did. All form fields that the server knows about have a name
attribute, and the Rails helper creates one. (View source in a browser
to see the generated code.)

<%= form_tag games_path, :method => ‘get’ do %>

<%= text_field_tag :search, params[:search] %> <%= submit_tag "Search", :title_german => nil %>

<% end %>

According to this, your field is named ‘search’, so you would change the
parameter that the “if” depends on accordingly:

if(params[:search])

params[:q] would be set if the text_field_tag looked like this:

text_field_tag :q, params[:q]

Walter

This is really weird. Having used “:q” as a name actually opens the “no
search yet” partial, wether I used the search function or not.

but using “:search” in my if clause I get a “NameError” in my index
file.

undefined local variable or method `games’ for
#<#Class:0x674f2c8:0x3f8e698>

Extracted source (around line #3):

1:


2:
3: <% games.each do |game| %>
4:
5:
6:

returning the “@” to “games” would cause a nomethod error for “each”…

Sorry to be bothering with this, but I’m really confused…

<%= link_to game.title_german, game %> <%= game.title_original %>

On Jan 30, 2013, at 1:01 PM, Ryo S. wrote:

This is really weird. Having used “:q” as a name actually opens the “no
search yet” partial, wether I used the search function or not.

but using “:search” in my if clause I get a “NameError” in my index
file.

undefined local variable or method `games’ for
#<#Class:0x674f2c8:0x3f8e698>

I made an error in my render partial code, it should read

render :partial => ‘games’, :locals => {:games => @games}

The missing symbol is what’s throwing this off. The fact that you see
something in the view when you use the wrong search symbol only tells
you that the if statement works.

Walter

First, thank you for helping me out with this problem for this long.

The Partial I’m using is “_search_results” not “_games”, so I corrected
this in my statement.

The if statement works indeed. Now the index page always shows my
desired template, when the search function was NOT used. But if it’s
used there is still an error. using “games” as a partial of course
throws me a “missing template” message, but actually using the partial I
need still throws me:

NoMethodError in Games#index

Showing …/app/views/games/_games.html.erb where line #3 raised:

undefined method `each’ for nil:NilClass

wether I use the “@” symbol or not in the search-field…

I feel bad not getting this straight. Even if I had come up with this
solution by myself I couldn’t handle this issue.

On 30 January 2013 22:15, Ryo S. [email protected] wrote:

NoMethodError in Games#index

Showing …/app/views/games/_games.html.erb where line #3 raised:

undefined method `each’ for nil:NilClass

wether I use the “@” symbol or not in the search-field…

I feel bad not getting this straight. Even if I had come up with this
solution by myself I couldn’t handle this issue.

How are you getting on with the Rails Guide on Debugging?

Another suggestion - work right through a good tutorial such as
railstutorial.org which will give you a good introduction to the
basics of Rails. Then you should understand better what is going on.

Colin

All right I fixed it now!!

Appearently all I had to put in the if statement, was the same code-line
I originally used for simply displaying the list:

def index

 @games = Game.search(params[:search])

end

I don’t even have to do an if-statement in the controller at all. That
goes to the html.erb, where either partial has to be displayed.

<% if(params[:search]) %>
<%= render ‘search_results’ %>
<% else %>
<%= render ‘no_search_yet’ %>
<% end %>

Otherwise, my “no_search_yet” partial would be displayed without
inhertiting any css…

Thanks anyway for your help guys, you were great!!

A far more greater issue would be the rating system I’m trying to
develop, but noone has replied in this thread so far :slight_smile: