On Jan 29, 2013, at 10:56 AM, Ryo Saeba wrote:
> 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
>
> [code]
>
> <% @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 %>
> [/code]
>
> 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.
>
> [code]
>
> <%= form_tag games_path, :method => 'get' do %>
> <p>
> <%= text_field_tag :search, params[:search] %>
> <%= submit_tag "Search", :title_german => nil %>
> </p>
> <% end %>
> [/code]
>
> My Model and my controller are defined as follows:
>
> [code]
> 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
> [/code]
>
> 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
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
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
No comments:
Post a Comment