Notes

This is a collection of my notes while I'm learning RoR… it will probably turn into a beginners tutorial.

Edit: I didn't really like rails. It wasn't really Rails, but Ruby that I didn't like. I've been using Django (Python Based) for a while now.

Establishing Relationships Between Tables

This example is going to assume we are creating an address book. It will be a simple address book… something like this:

John Doe
123 Main St
Raleigh, NC 27603

Note that I didn't actually create an address book while creating this document… I'm just using it as a general example since an address book works nicely with the ideas I want to present.

Create the SQL Schema

We'll use two tables for this… one with the address and a lookup table for the state. So the tables will look something like this:

address

id (int)
name (varchar)
street (varchar)
city (varchar)
state_id (int)
zip (int)

state

id (int)
abbreviation (varchar)
name (varchar)

Generate the Base Files

Generate the files using the following commands (remember that the table should be singular):

script/generate scaffold Address
script/generate scaffold State

Define The Relationships

Edit addressbook/app/models/address.rb

class Address < ActiveRecord::Base
        belongs_to :state
end

Edit addressbook/app/models/state.rb

class State < ActiveRecord::Base
        has_many :addresses
end

Edit the Controller

As you can see from your site, it still isn't showing the state. The reason is because the Address Controller and View still needs to be told what to do with the state.
Since you've already created the controller with the scaffolding, most of the code is already there… we just need to edit it. We want to show all of the states in an HTML SELECT box, so we're going to need to query the database for all of the States.

Edit addressbook/app/controllers/addresses_controller.rb

Look for the following snippet:

  def edit
    @location = Location.find(params[:id])
  end

You just need to add one line:

def edit
  @location = Location.find(params[:id])
  @state = State.find_all
end

What that does is lookup all of the states and assign them to the @state array. Now we can use that array within the html template.

Edit the Views

The Edit Form

The scaffolding creates the form elements within the _form.rhtml file within the views directory. Let's take a look:

Edit addressbook/app/views/addresses/_form.rhtml

<%= error_messages_for 'address' %>

<!--[address]-->
<p><label for="address">Name</label><br/>
<%= text_field 'address', 'name'  %></p>

<p><label for="address_street">street</label><br/>
<%= text_field 'address', 'street'  %></p>

<p><label for="address_city">City</label><br/>
<%= text_field 'address', 'city'  %></p>

<p><label for="address_zip">Zip</label><br/>
<%= text_field 'address', 'zip'  %></p>

<!--[eoform: address]-->

As you can see, no state! Add the following line after the city:

<p><label for="address_state_id">State</label><br/>
<%= collection_select('address', 'state_id', State.find_all, 'id', 'name') %></p>

Editing show.rhtml

Now you can edit it, but you still need to update show.rhtml so that the state shows up when viewing the record. You'll have to scrap all of the scaffolding here and write most of the file from scratch.

It should look something like this:

<p>
<b>Name:</b> <%= @address.name %><br>
<b>Street:</b> <%= @address.street %><br>
<b>City:</b> <%= @address.city %><br>
<b>State:</b> <%= @address.state.abbreviation %><br>
<b>Zip:</b> <%= @address.zip %>
</p>
 
<%= link_to 'Edit', :action => 'edit', :id => @address %> |
<%= link_to 'Back', :action => 'list' %>

Editing list.rhtml

Of course, if you want to show the state up in the general list, you'll have to edit it to. I have no need for that now, so I'm not gonna bother with it. It should be the same idea behind editing show.rhtml.

 
ruby_on_rails.txt · Last modified: 2007/09/10 13:05 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki