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

Forms and Routing in RoR

2006-03-06   [ 0 comments ]

Wow, day two with the web application framework Ruby on Rails and things are just starting to heat up around here. My keyboard is still fresh and unworn, but somehow I have a complete, simple yet highly efficient blog manager (that looks nice too!). So many things seem to draw my attention deeper into RoR, perhaps it is the language itself (Ruby that is), or perhaps it has alot to do with the Model, View, Controller seperation of logic, data, and display. Whatever the case, I am quickly becoming a fan of this environment.

I really want this site to be my chance to share with everyone the bumps, surprises and excitement I experience along the way learning this great new tool. So here I am ready to share the next portion of my journey up the learning curve...

What better way than to discuss the intricacies of a most often overlooked but always necessary HTML object. The form. Yes, those wonderful places where data can be easily transferred back and forth, updated and saved. It all starts with forms, and RoR makes form creation and processing a breeze.

Initially I thought that forms would be quite the same as in HTML (besides forms in PHP pages were just that- HTML forms excluding the embedded PHP). However I was mildly surprised to find that RoR has a complete new set of tags for form creation.

I was at first a bit reluctant, but after I got used to the following syntax it wasn't that hard to use (thanks to the Rails API of course):

<%= start_form_tag :action => 'some_method', :id => @thought %>
<%= text_field 'thought', 'title', :size => 20 %>
<%= submit_tag 'Save Thought!' %>
<%= end_form_tag %>

That's it! Of course that is a basic form - it only contains one text field, but hey its a start. The :action tells the form what method in the controller will be handling the request. The :id is set if there is a record to update or view. In Example, if I had passed an :id in the controller I would have done something such as the following in the controller:

@thought = Thought.find(params[:id])

With that instance variable (@thought) the form would be populated with values to edit / update. Pretty simple, right!

Perhaps another tidbit of useful knowledge has to do with setting a default controller. What is that you might ask? Well, I didnt know what it was until one puzzling question started to get me:

Why doesn't my domain resolve to some controller without having to use the index.html file in the default web directory?

My friend Jim suggested I look into the routes.rb file under the config main directory. After a quick look inside I realized what needed to be done. Simply add an entry in the routes.rb file like the one below and remove the index.html file from your public web directory - and bam! your domain will resolve directly to the controller you specified!

map.connect '', :controller => "user"

Now things are starting to shape up around here! Time to get back to the IDE :-)

: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"