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.
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!
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.
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…
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
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:
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
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
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
Have a look at the Rails Guide on Debugging. It will show you
techniques that you can use to debug the code.
"
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
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.
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.)
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.
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.
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.