RuntimeError in Recipe#edit


#1

I have tried to learn ruby on rails from scratch by going into a
tutorial at the following website.

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

I followed the suggestions and encountered 3 problems so far.

  1. Couldn’t start the Apache server. Error message “Port 80 was used by
    some other process.” I over-came the problem by going

into configuration and change the port to 4001.
2. I couln’t connect to my localhost using the MySQL-Front. I overcame
the problem by following the explanation in one

website and run RegEdit and changed some setting in the Windows
Registration.
3. I copied and pasted the codes for edit.rhtml, and run the example and
found that text boxes were not filled with the

desired fields, but the source codes instead. I deleted every thing and
re-installed every thing, and came to the same

problems in “Edit”.

The error message is now 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

It appears that @recipe is null.

Any assistance will be appreciated.

Regards

Ling


#2

On 5/7/06, Ling removed_email_address@domain.invalid wrote:

into configuration and change the port to 4001.

Extracted source (around line #8):

Any assistance will be appreciated.

Regards

Ling

What URL were you using when you got this error? The URL will tell you
the
id of the recipe you are trying to edit. For example if the URL was :

http://127.0.0.1:3000/recipe/edit/2

Then you would be trying to edit the recipe with id=2. Once you know the
id,
go look at your database. Do you have a recipe with that id?

If so then there is something wrong with your controller because it
should
be setting @recipe.

Also, Instant Rails comes with a copy of the cookbook application
preinstalled. Can you run this one? If so, you can always compare your
source code with that.

Curt


#3

Thanks for your reply.

I first clicked on â??http://localhost:3000/recipe/listâ? that gave me the
display, in website called â??http://localhost:3000/recipe/listâ

Then I clicked on one of the items, say, the first one in the grid.

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

So far, so good.

Then I clicked on the â??Editâ? button at the bottom:

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”}

p.s.

The html codes for Edit from <Form â?¦ to in both â??cookbookâ? and
â??cookbook2â? are almost identical. Even after I amended the codes to be
identical with the pre-installed, the problem persisted.

Regards,

Ling.

Curt H. wrote:

On 5/7/06, Ling removed_email_address@domain.invalid wrote:

into configuration and change the port to 4001.

Extracted source (around line #8):

Any assistance will be appreciated.

Regards

Ling

What URL were you using when you got this error? The URL will tell you
the
id of the recipe you are trying to edit. For example if the URL was :

http://127.0.0.1:3000/recipe/edit/2

Then you would be trying to edit the recipe with id=2. Once you know the
id,
go look at your database. Do you have a recipe with that id?

If so then there is something wrong with your controller because it
should
be setting @recipe.

Also, Instant Rails comes with a copy of the cookbook application
preinstalled. Can you run this one? If so, you can always compare your
source code with that.

Curt


#4

Are you able to run the preinstalled version of cookbook? If yes, then
there’s got to be some subtle difference between you version and it
(either
in the code, or in the database).

Curt


#5

Curt H. wrote:

Are you able to run the preinstalled version of cookbook? If yes, then
there’s got to be some subtle difference between you version and it
(either
in the code, or in the database).

Curt

Thanks. Curt.

I tried the pre-installed “cookbook” with the exisitng data.

I was able to create new categories, new recipes, and edit and delete.
No problems.

I removed the “cookbook2” database and re-created the database, taking
care to see the tables and fields are the same as those in “cookbook”

I copied the whole folder App from “cookbook” to “cookbook2” and so the
codes are identical.

With the empty database, I now tried to display the recipes (none
entered as yet), and an error message appeared.

So I load the website to enter 3 categories successfully.

I then entered into the web page to create a new recipe. The page loaded
successfully with 3 items in the drop-down list for categories. I
entered data into the text boxes and text-area, and selected one item of
category from the drop-down and hit the “Create” button.

Unable to save data.

Errors encountered. Same as when I tried to display the recipe when
there were none.

Please advise what to do from now.

Regards,

Ling.


#6

Great!


#7

Curt H. wrote:

Are you able to run the preinstalled version of cookbook? If yes, then
there’s got to be some subtle difference between you version and it
(either
in the code, or in the database).

Curt

Hi, Curt

I found my mistake in the database setting.

The field “title” in the table [recipes] doesnot allow null and I set
the default to [null].

Once I corrected this mistake, the application is running.

Thanks for your advice that led me me to discover the mistake.

Now, I can proceed to a new tutorial.

Regards,

Ling.