<— TutorialStepFour / TutorialStepFive | Tutorial | TutorialBasicRelational —>
Change the view (see UnderstandingViews) app/views/friends/view.rhtml created in TutorialStepThree. Open up the file in your favorite editor and change it to this: (feel free to decorate it with HTML if your creative side urges you to)
Friends#view
This page will display one friend
<%= @person.name %>
<%= @person.street1 %>
<%= @person.street2 %>
<%= @person.city %>
<%= @person.state %>
<%= @person.zip %>
Note how it is trying to spit out some dynamic content. The @person 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:
def view @person = Person.find(1) end
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:
class Person < ActiveRecord::Base end
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. This doesn’t just mean adding an ‘s’. Irregular plural forms are also handled, so Rails knows that the plural of ‘person’ is ‘people’. The rules for how it does this are described in the documentation
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:
ruby script/server
or
./script/server —environment=production
for production environment under *nix
Point your browser to http://RAILS/Friends/view (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 (@view@) in it!
For this to work, you will need to create a database named rails_development and add the people table. (Make sure not to set the autoincrement to 2!) After that, you’ll need to add one record.
Go on to TutorialBasicRelational | Tutorial
Or, if you skipped it, TutorialStepFive (Testing)
Comment: (by Tony Thompson)
If you are having problems with the DB connection, added ‘socket: /var/lib/mysql/mysql.sock’ to each db connection group in the database.yml file. You’re socket may be in a different location (this is fedora’s location). Use ‘locate mysql.sock’ to find your socket on linux system. Unix systems without locate will need to use find.
There are concerns below with installing mysql through gem. On current versions of Fedora, this is not easy to do and is not nessary.
display to view to avoid Gotcha mentioned below. show is also an alternative name but this is handled later in the tutorial.
Comment: Does anyone know why the bit about adding model :friend 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.
Reply: Since model :friend 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.
Comment: when I do try to display the page ‘friends/display’ all I get is an error dump:
" Showing /friends/display.rhtml where line #6 raised undefined
method `name' for nil:NilClass
3: <h1>Friends#display
4:
This page will display one friend
5:
6: <= @person.name %>
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.
Reply: 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.
Reply-Reply: 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.
Comment: I am using Apache as the server and I get the index page as expected with http://localhost. But when I do try to display the page with http://localhost/friends/view I get a ‘404 – Not found’. I followed the tutorial and all the files and codes are exactly as given. Can anyone tell me where I must have gone wrong?
Reply: Try visiting http://localhost/public/friends/view
Reply: The http://localhost/public/friends/view doesn’t work (for me, at least) either. On Windows, what I had to do is edit %SystemRoot%\system32\drivers\etc\hosts and add a line for “127.0.0.1 rails” (where rails is the ServerName from the httpd.conf VirtualHost entry).
Reply: Make sure you have all of the paths set right in the vhost config.
Comment: I am using Apache and MySql, and I get the following error. Any clues? I know the database, the table, and the entry are all there.
Errno::ENOENT in Friends#view
No such file or directory - /tmp/mysql.sock
RAILS_ROOT: ../config/..
Application Trace | Framework Trace | Full Trace
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/vendor/mysql.rb:104:in `initialize'
...
...
...
#{RAILS_ROOT}/app/controllers/friends_controller.rb:7:in `view'
Reply: I had the same problem, until I read the database.yml file:
- Install the MySQL driver:
- gem install mysql
That fixed it for me.
Reply:Reply: gem install mysql doesn’t work correctly for me
Building native extensions. This could take a while...
- extconf.rb failed *
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. You may need configuration options.
I suspect that I need to upgrade to mysql5, but that’s a whole another issue since mysql is not yet packaged for ubuntu.
category:Tutorial
Reply:Reply:
on fedora, yum -y install ruby-mysql.i386 is more likely to work.
Reply:Reply:
Actually it’s a compiler issue. Make sure you locate the right mysql_config. I followed the instructions here: http://ocsforums.com/printthread.php?t=132 .
Comment
what a mess! Followed everything to the letter and I just get a 404 Not Found.. I’ll try again next year.
Reply to: No such file or directory – /tmp/mysql.sock
Into database.yml you need add the socket param and write the correct path to mysqld.sock.
development:
adapter: mysql
socket: /var/run/mysqld/mysqld.sock
database: ficha_development
username: carlos
password:
host: localhost
I’ll second the mess statement, it should say at the start what level it is, also there are too much assumption, a tutorial is expected to be followed to the letter, if we do that we get errors.
Comment by Ryan Hennig
I keep getting an Application Error when I browse to http://localhost/friends/view. When I check the development.log I see:
Recognition failed for "/view"
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/routing.rb:488:in `recognition_failed'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/routing.rb:478:in `recognize!'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/dispatcher.rb:38:in `dispatch'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:150:in `process_request'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:54:in `process!'
/usr/local/lib/ruby/site_ruby/1.8/fcgi.rb:600:in `each_cgi'
/usr/local/lib/ruby/site_ruby/1.8/fcgi.rb:597:in `each_cgi'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:53:in `process!'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:23:in `process!'
/Users/rhennig/Rails/friends/public/dispatch.fcgi:24
I got a similar error before, so I followed the RoR setup instructions on Tony Arnold’s page and I still get the same error.
Comment by Ryan Hennig
I fixed this by browsing to http://localhost/friends/friends/view instead. Then I had to fix some permissions in tmp/sessions.