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

On Tuesday, January 29, 2013 9:56:54 AM UTC-6, Ruby-Forum.com User 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 
>
> -- 
> 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+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msg/rubyonrails-talk/-/hHFt6uIdoggJ.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to