My First Steps with Ruby On Rails

It is time I become acquainted with the Ruby programming language. It served me well on a recent project and my friends at Indaba Music love it.

Installation on a Mac

I did try the following, but I think actually you can go straight to the tutorial and follow the steps there.

  1. Download the latest zip file
  2. Unpack into a directory and move into your directory
  3. Open a Terminal
  4. cd from your home directory into the new directory  (cd rubygems-1.3.X/)
  5. run: sudo ruby setup.rb

Warning

It appears that Ruby on Rails has changed a lot between versions. So starting the server was harder than the Internet said it would be (as script/server approach has become defunct). So I spent some time getting confused by online tutorials, such as Apple. Installing XCode from the Apple Developer Center however was useful.

Learning Rails

I have found much more joy by following the tutorial at http://guides.rubyonrails.org/getting_started.html and a basics tutorial. Oh and my good friend jcn helped explain some stuff too.

Creating a Basic Form

Next I wanted to create a basic HTML Form where the Controller catches and processes the results. I got most of what I needed from an online answer. However I needed to know that creating the controller is done at command line using

rails generate controller Searches

and then place in the following code.

class SearchesController < ActionController::Base

 

def search

lookup = params[:lookup]

@models = Model.find_by_lookup(lookup)

end

end

And place in the following code for the view.

<% form_tag searches_path do %>
<label for="lookup">Lookup</label>
<%= text_field_tag :lookup %>
<%= submit_tag "Submit" %>
<% end %>

But the stumbling block for me was linking the View to Controller by placing the following in the config/routes.rb file

resources :searches