On Mar 9, 2006, at 9:35 AM, Rich Hall wrote:
> I am very new with Ruby on Rails and most coding in general. I have
> created a DB table with birthdays and names. The birthdays are split
> into two fields (Month and Day). Using this table I would like to
> display birthdays for the current month.
>
> How can this be done?
>
Pretty broad question, but I might be able to take a quick gander.
First, assuming you''ve got your database.yml file set up to connect
to your database properly, add a controller, something like:
# app/controllers/birthdays_controller.rb:
class BirthdaysController < ApplicationController
def show
@people = Person.find_all_by_month(params[:month])
end
end
The above code assumes that (a) you have a model called Person that
(probably) maps to your DB table is called "people", and (b) that you
have a route set up in routes.rb, like so:
map.connect ''birthdays/show/:month'', :controller =>
''birthdays'', :action => ''show''
Next, you''d create the view for the ''show'' action
inside app/views/
birthdays/show.rhtml:
<ul>
<% for person in @people %>
<li><%= person.name %>: <%= person.month %> / <%=
person.day %></li>
<% end %>
</ul>
Then you should be able to visit your view (once your server is
running) at http://localhost:3000/birthdays/show/8 (e.g. show August
birthdays).
Duane Johnson
(canadaduane)
http://blog.inquirylabs.com/