Still Stumbled on First things in trying RubyOnRails


#1

Still Stumbled on First things in trying RubyOnRails

http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html

http://instantrails.rubyforge.org/tutorial/index.html

http://rails.homelinux.org/

I had tried the first of the top 12 tutorials and got stuck after adding
a new field â??category_idâ?in the table recipes, introduced
C:\InstantRails-1.0\rails_apps\cookbook2\app\views\recipe\edit.rhtml,
and modified
C:\InstantRails-1.0\rails_apps\cookbook2\app\controllers\recipe_controller
C:\InstantRails-1.0\rails_apps\cookbook2\app\models\category
C:\InstantRails-1.0\rails_apps\cookbook2\app\models\recipe

I log into http://localhost:3000/category/list, chose an item, and when
I hit the â??Editâ? button, error message occurred.

I then went on to modify
C:\InstantRails-1.0\rails_apps\cookbook2\app\views\recipe\list
C:\InstantRails-1.0\rails_apps\cookbook2\app\views\recipe\edit

And now I couldnâ??t even log into http://localhost:3000/category/list.

After some discussions in the forum, I removed the database, re-created
a database â??cookbook2â? that resembled â??cookbookâ?, and copied all the
codes from â??cookbookâ? into the â??cookbook2â? directory.

With the full set of codes in place, and a new, empty database, I went
about creating, editing, deleting categories. No problems.

I went on to create, edit, and delete recipes. No problems.

My conclusion; with the full codes in place, even starting from an empty
database, the application is working fine.

After that, I removed the database, and the â??cookbook2â? directory, and
re-run the tutorial from scratch.

You know what, the same old problems occurred at the point when â??Editâ?
was introduced.

Introducing the field â??category_idâ? and changing
C:\InstantRails-1.0\rails_apps\cookbook2\app\models\category and
C:\InstantRails-1.0\rails_apps\cookbook2\app\models\recipe is a bit like
introducing a field with foreign-key contrainst.

Would mySQL automatically fill up this field in the exisitng records
when the amended codes are run?

If not, how do I fill in the blanks?
The tutorial had been there for many months, and I am sure many people
had taken it and there was no complaint.

So, what did I do wrong? Why me?

I would be grateful if some one would help.

After that, I went on to try â??Four Days on Railsâ? tutorial.

I was stuck because I donâ??t know how to introduce the Apache that is
required. So I used the InstantRails 1.0 that is already installed.

When I tried to log on http://todo/ I didnâ??t get the expected
â??Congratulations â?¦â?

As I had changed the port in Apache Configuration to 4001, and Apache is
listening to Port 80, I couldnâ??t go any further.

When I changed the Apache port back to 80, Apache couldn’t started.
Error message said that Port 80 is used by some other process.

As you can see by now, I am new to the Apache, mySQL, and even a bit shy
in Java Runtime, is it possible for some one to guide me through the
initial set-up and installation of Ruby, Rails, Apache, and mySQL
including the setting and changing of environment variables, path, lib,
etc, so that I can get into tutorials like the top 12?

Your assistance will be appreciated.

Ling.


#2

I log into http://localhost:3000/category/list, chose an item, and when
I hit the â??Editâ? button, error message occurred.
Write the error message here so it would be easier to identify the
problem.

After that, I went on to try â??Four Days on Railsâ? tutorial.

I was stuck because I donâ??t know how to introduce the Apache that is
required. So I used the InstantRails 1.0 that is already installed.

When I tried to log on http://todo/ I didnâ??t get the expected
â??Congratulations â?¦â?

You don’t have to use Apache. I used WEBrick from InstantRails and I
finished the â??Four Days on Railsâ? tutorial. Just remember that you can
only use WEBrick with one app at a time. To run WEBrick, type ruby
script\server in the todo directory. Then go to http://localhost:3000.

You can use phpMyAdmin (which is included in InstantRails) for creating
your database and tables. Go to http://localhost/mysql. Make sure apache
is running. It automatically starts when you start InstantRails.

Take note that I used two servers when I was reading the tutorial.
WEBrick is used for the ToDo app, and Apache is used for phpMyAdmin.

I also tried using Apache for my web apps and it worked, but WEBrick is
fine and I think is even recommended.

-topher


#3

topher wrote:

Write the error message here so it would be easier to identify the
problem.

Thanks.

I have un-installed and deleted all. No examples are currently
available.

A previous error message is reproduced as follows:

That gave a url of http://localhost:3000/recipe/edit/1

Together with the error message as follows:

RuntimeError in Recipe#edit
Showing app/views/recipe/edit.rhtml where line #8 raised:
Called id for nil, which would mistakenly be 4 – if you really wanted
the id of nil, use object_id
Extracted source (around line #8):
5:
6:

Edit Recipe


7:
8:
9: <input id=“recipe_id” name=“recipe[id]” size=“30”
10: type=“hidden” value="<%= @recipe.id %>" />
11:

Title

RAILS_ROOT: ./script/…/config/…
Application Trace | Framework Trace | Full Trace
#{RAILS_ROOT}/app/views/recipe/edit.rhtml:8
Request
Parameters: {“id”=>“1”}
Show session dump

flash: !map:ActionController::Flash::FlashHash {}

Response
Headers: {“cookie”=>[], “Cache-Control”=>“no-cache”}

It appeared to me that when I made use of the full sets of codes and an
empty database, and build up from categories to recipes, everything is
working.

But if we introduce a new field with foreign key constraint, and modify
the codes mid-stream, things start to break down as in the 2 tutorials I
tried.

I might have missed out some steps, like filling in the category_id,
off-line, and then re-start the application.

I don’t know.

Regards,

Ling.


#4

topher wrote:

You probably missed a few steps. Adding new columns and even foreign
keys on your tables shouldn’t be a problem in Rails. Just put belongs_to
and has_many and Rails will handle everything.

Here are the steps you should pay attention to.

  1. create category_id field in the recipes table. type should be the
    same as id in the category table
  2. edit recipe and category models. put belongs_to and has_many
  3. in recipe_controller.rb, write the edit method
  4. create edit.rhtml

Then test your app.

Good luck.

-topher

Thanks.

Rails4days

I was working through the â??Four days on RORâ? tutorial. I was doing fine
from day 1 to 2.

From Day 3 onwards there were so many changes. I like the explanations.

So, I copied and pasted the codes from the PDF.

At the end of the whole process, when I ran the app there were quite a
few errors, mostly on wrong syntax, or something in the wrong place or
something missing. So, some codes were not copied, or the syntax were
wrong as I pasted the amended at wrong places, or put an â??endâ? where it
is not required.

I guess I did learn something in the process.

At the end, I got impatient.

I just copied the downloaded codes into the required directories. It
took less than 2 minutes.

When I ran the app again, well, it was working.

:wink:

I thought so, anyway. I noted that the images were represented by X. I
then realized I didnâ??t copy the image files. Since it was late, I
switched off the PC and went to bed.

The next morning, I hopefully turned on the app. To my horrors, it
wasn’t working.

In particular, the Webrick was not working properly.

I deleted the directory, reinstalled the InstantRails 1.0, and later
1.3.

When I copied and pasted only a few modules, in the early part of the
tutorial, things worked.

When I copied all the down-loaded files and pasted into the directory,
nothing seemed to work.

Am I supposed to work through the modules step by step, and not
encouraged to copy and paste the codes?

Can someone help to explain why the Webrick didnâ??t work?

Regards,

Ling.


#5

That gave a url of http://localhost:3000/recipe/edit/1

Together with the error message as follows:

RuntimeError in Recipe#edit
Showing app/views/recipe/edit.rhtml where line #8 raised:
Called id for nil, which would mistakenly be 4 – if you really wanted
the id of nil, use object_id
Extracted source (around line #8):
5:
6:

Edit Recipe


7:
8:
9: <input id=“recipe_id” name=“recipe[id]” size=“30”
10: type=“hidden” value="<%= @recipe.id %>" />
11:

Title

RAILS_ROOT: ./script/…/config/…
Application Trace | Framework Trace | Full Trace
#{RAILS_ROOT}/app/views/recipe/edit.rhtml:8
Request
Parameters: {“id”=>“1”}
Show session dump

flash: !map:ActionController::Flash::FlashHash {}

Response
Headers: {“cookie”=>[], “Cache-Control”=>“no-cache”}

I think the error means that @recipe in line 8 is nil when it shouldn’t
be. Check the edit method in recipe_controller.rb.

It appeared to me that when I made use of the full sets of codes and an
empty database, and build up from categories to recipes, everything is
working.

But if we introduce a new field with foreign key constraint, and modify
the codes mid-stream, things start to break down as in the 2 tutorials I
tried.

I might have missed out some steps, like filling in the category_id,
off-line, and then re-start the application.

I don’t know.

You probably missed a few steps. Adding new columns and even foreign
keys on your tables shouldn’t be a problem in Rails. Just put belongs_to
and has_many and Rails will handle everything.

Here are the steps you should pay attention to.

  1. create category_id field in the recipes table. type should be the
    same as id in the category table
  2. edit recipe and category models. put belongs_to and has_many
  3. in recipe_controller.rb, write the edit method
  4. create edit.rhtml

Then test your app.

Good luck.

-topher