On Jul 3, 2012, at 1:16 PM, sehrguey o. wrote:

> Walter,
> here is the contents of my disk.rb
> -----------
> class Disk < ActiveRecord::Base
>  attr_accessible :director, :language, :notes, :starring, :title, 
> :volume_name, :wrapper
> 
>  def self.search(search)
>    if search
>      find(:all, :condition=>['name LIKE ?', "%#{search}%"])
>    else
>      find(:all)
>    end
>  end
> end
> ------------

This is putting the search method where it belongs, in the model, rather than 
inline in the controller, as I had it. Completely identical in every functional 
way, just better MVC layout this way. So the only other question here is, do 
you have a form on your index page layout that can set a params[:search] with 
the desired search term? If you've looked at the RailsCast, there's a nice easy 
snippet there to make it with the form_tag helper (useful because you don't 
have an associated model). Can you show that code here?

You want to issue a GET request from the form to the index method, and then 
this will all just work. Your self.search method will default to gathering all 
records, because the query is 'WHERE name LIKE "%%"', which matches everything 
in the table. When you do pass a querystring to it that's like  ?search=foo, 
then you will get the records where name contains foo. This means that there's 
no need for an additional controller method or a separate route or template for 
search results. If you do want to specialize the results page, you could do 
something in the index.html.erb template to switch on the presence of a 
params[:search] variable, and use that to change the page title or header or 
both.

Walter

> 
> yours`
> sehrguey
> 
> -- 
> 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 post to this group, send email to rubyonrails-talk@googlegroups.com.
> To unsubscribe from this group, send email to 
> rubyonrails-talk+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/rubyonrails-talk?hl=en-US.
> 

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en-US.

Reply via email to