<— TutorialStepFour / TutorialStepFive | [[Tutorial]] | TutorialBasicRelational —>

h2. Develop your Rails application!

Open up the @app/views/friends/view.rhtml@ file in your favorite editor. The previous step created this for us, and we need to fill it out. Change it to this: (feel free to decorate it with HTML if your creative side urges you to)

<pre>
<html>
<body>
<h1>Friends#view</h1>
<p>This page will display one friend</p>
<p>
<%= @person.name %><br />
<%= @person.street1 %><br />
<%= @person.street2 %><br />
<%= @person.city %><br />
<%= @person.state %><br />
<%= @person.zip %><br />
</p>
</body>
</html>
</pre>

Note how it is trying to spit out some dynamic content.  The <code>@person</code> object needs to be created by the controller object, so open up @app/controllers/friends_controller.rb@. 

Notice that the methods have already been created for you. 

Find the view method, and replace it with:

<pre>
def view
  @person = Person.find(1)
end
</pre>

This will pull the first record out of the database and put it in the @person variable for the view to use.  

Now we need to look at that model class more carefully, so open up @app/models/person.rb@. You should see the skeleton class that was created when you ran the new_model script in TutorialStepFour:

<pre>
class Person < ActiveRecord::Base
end
</pre>

Because the Person class subclasses \ActiveRecord::Base, most of the work is done for you already. For example, accessor methods will be dynamically generated, so you don't have to write those yourself.  

How does this know to map to the people table we created?  ActiveRecord pluralizes the class name and looks for that table in the database.  The rules for how it does this are described in "the documentation":http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M000250

Make sure you've performed all the steps in GettingStartedWithRails to get Apache set up.  Note that you can alternatively run the WEBrick servlet with: 

<pre>
ruby script/server
</pre>

or
<pre>
./script/server --environment=production
for production environment under *nix
</pre>

Point your browser to @http://RAILS/friends/view@ url (where @RAILS@ is the hostname and base URL path to the rails installation), and you should now be able to see your  application at work! Note how the URL has both the controller (@friends@) and the action (@show@) in it!

Go on to TutorialBasicRelational | [[Tutorial]]
Or, if you skipped it, TutorialStepFive (Testing)

<hr>
<b>Comment:</b> Changed @display@ to @view@ to avoid [[Gotcha]] mentioned below.  @show@ is also an alternative name but this is handled later in the tutorial.

<hr>
<b>Comment:</b> Does anyone know why the bit about adding <code>model :friend</code> was removed from this?  Am I just using an older version of rails and the new version adds this line automagically?  I'm using the current version according to gem as of May 5.
<b>Reply:</b> Since <code>model :friend</code> seems to be used to declare the model so that a session reference can be maintained it is likely that it was just adding complexity where it was not required. See WhenToUseTheModelMethod for further discussion on the matter.

<hr>
<b>Comment:</b> when I do try to display the page '@friends/display@' all I get is an error dump:<p>
<pre><code>
" Showing /friends/display.rhtml where line #6 raised undefined 
method `name' for nil:NilClass
3: <h1>Friends#display</h1>
4: <p>This page will display one friend</p>
5: <p>
6: <%= @person.name %><br />
7: <%= @person.street1 %><br />
8: <%= @person.street2 %><br />
9: <%= @person.city %><br />"
</code></pre>
Obviously @@person@ is nil, but I have no idea why. Followed the tutorial and the database query works at the command line.

<b>Reply:</b> This is because the view is called display. Changing this and the appropriate view file to something else (e.g. display2 and display2.rhtml), causes the page to load properly. It's a pretty common [[Gotcha]].

<b>Reply-Reply:</b> Yes this works: edit apps/controllers/friends_controller.rb and change 
the line:
'def display' to 'def display2'. Then rename the file app/views/friends/display.rhtml to app/views/friends/display2.rhtml and this ought to work.

category:Tutorial
