Caution! - Many of these posts are creepy-old in the Ruby on Rails world (before 1.0!)
The :author => Charles Abbott now blogs here

If Elsif Else, and Searching Too!

2006-03-17   [ 0 comments ]

Happy St. Patrick's day! Today I'm am going to leave another quick post, but rest assured, I will post another update this weekend. Ruby on Rails is really showing some admirable characteristics for its ease of use. I am going to quickly go over conditional statements (in their simplest form) and a quick implementation of a search box that utilizes ajax (you should see it on this page).

The first part is going to be quick and simple, but hopefully it might save someone some time. For every "if" should have a corresponding "end" tag. If you require an "else" just put it in the middle somewhere. And finally - the one that got me is:

An else-if is entered exactly as so: elsif (without the e!!!)

This little detail had me held up for a good ten minutes (until I did a quick google). It is a slight annoyance, but sense it does remove the need for one more letter I can't complain too much. However, my ide RadRails highlighted it everytime it typed else-if, so I thought it wasn't the problem for those few minutes.

As for searching, allow me to briefly go over what you will need (which includes that really neat ajax effect). So lets start, first off - we need to include the javascript files into our layout (or view, I chose the layout for all my views). Just insert the following into the layout:

<%= javascript_include_tag :defaults %>

That short include tag brings in all the necessary javascript files - now we need to add a form in the view we want to search from (my search form is in the page I want to search through - the index page). So inside your form tag use this snippet to prepare your view for searching:

<%= text_field_with_auto_complete :thought, :keywords %>

Where :thought is the model/table you wish to search for matching objects, and :keywords is the field you wish to perform your search on.

Now you have the front peices ready to go, and if you were to try and use them, you would unfortunately get nowhere. This is where we will need to add a special RoR tag into our controller that handles this request. So in my corresponding controller I will put this one line outside of any method:

auto_complete_for :thought, :keywords

In my setup, the form is submiting the search data to the same method (the index method). To handle the search terms (and find the corresponding records) I setup my index method like so:

def index

if !params[:thought]
@posts = Thought.find(:all, :order => "id DESC")
else
@posts = Thought.find(:all, :conditions => ["keywords = ?", params[:thought][:keywords]])
end
end

So I check to see if there were submitted params then query the database to retrieve the records with the exact keywords. Simple enough!

There is one problem with this setup - My users have to submit the exact keywords (ie, they have to select one of the selections the ajax call presents). This is a poor design, and needs improvement, but for the sake of learning how to make the ajax search happen, it will do fine for now.

Perhaps next post I can go over how to improve the search feature. In the meantime check out the new Rails Recipes book over at the Pragmatic Programmer book store.

:author => "Charles Abbott"
Converting to Ruby on Rails
 

What?

Who?              Link?



Frameworks Good or Bad?   :date => "2007-10-06"
Where is ForTheCode.com Going?   :date => "2007-09-23"
Refactoring - Vital to Software Development   :date => "2007-09-23"
Mongrel Cluster a quick note - and extra notes   :date => "2007-05-20"
Linux Mongrel and Rails   :date => "2007-05-15"
form_remote_tag revisited   :date => "2007-01-07"
How To: Ubuntu 6.10 Edgy on Rails part 3   :date => "2006-12-30"
How To: Ubuntu 6.10 Edgy on Rails part 2   :date => "2006-12-24"
How To: Ubuntu 6.10 Edgy on Rails   :date => "2006-12-22"
verify ... 5.times do cycle   :date => "2006-09-25"
country_select, country_options_for_select, mail_to   :date => "2006-09-05"
Generate and Send Email in Rails   :date => "2006-08-26"
FDF Model, gsub, and send_data   :date => "2006-08-18"
Active Directory Authentication with acts_as_authenticated   :date => "2006-08-17"
Apache2 proxy with Lighttpd - FastCGI for Rails   :date => "2006-08-08"
reverse! && a simple file Upload Class   :date => "2006-07-29"
send_file - a link to download a file   :date => "2006-07-24"
Environments (production, development, test) and cache_pages   :date => "2006-07-04"
.class .methods .instance_variables   :date => "2006-06-14"
select_tag :multiple => true   :date => "2006-06-01"
FileUtils, action_controller rescues   :date => "2006-05-20"
file_field_tag, File.size, File.path, FileUtils.mv   :date => "2006-05-15"
javascript_include_tag, stylesheet_link_tag   :date => "2006-05-02"
submit_to_remote, form_remote_tag, script.aculo.us   :date => "2006-04-30"
periodically_call_remote, simple_format   :date => "2006-04-26"
observe_field - Ajax!   :date => "2006-04-21"
h method, TextHelper, sanitize(), strip_tags()   :date => "2006-04-15"
Rails API :My API [.count(), link_to, text_area :size]   :date => "2006-04-13"
Rails - HTML Select Tag   :date => "2006-04-05"
Pruning Old Sessions   :date => "2006-03-21"
If Elsif Else, and Searching Too!   :date => "2006-03-17"
SHA1 - A quick update   :date => "2006-03-15"
Initialized! good, Authorized? Great! part 2   :date => "2006-03-11"
Initialized! good, Authorized? Great!   :date => "2006-03-08"
Forms and Routing in RoR   :date => "2006-03-06"
My First RoR Post !   :date => "2006-03-05"