Routing problem following tutorial rails-20-and-scaffolding-step-by-step.html

Hi,

I’m following the excellent tutorial
fairleads: Rails 2.0 and Scaffolding Step by Step.
I apparently have a routing problem.

I followed every step precisely until:
ruby script/generate scaffold Movie title:string description:text
one_sheet_url:string

I opted for:
ruby script/generate scaffold Stocks symbol:string name:string,
which produced essentially the same results as the tutorial.

Finally, the tutorial targeted “http://localhost:3000/movies” and got:
Listing movies
[snip]

I targeted “http://localhost:3000/stocks” and got the error output
shown below.

I scanned the step-by-step article for “rout” and found one response
to the article that suggested that routing should manually adjusted
and offered a link to a 5-part series on Rails 2.0 routing. While I
study that, can I get some suggestion(s) about how to address my
immediate problem?

Thanks in Advance,
Richard

http://localhost:3000/stocks

NameError in Stocks#index

Showing stocks/index.html.erb where line #22 raised:

undefined local variable or method `new_stocks_path’ for
#ActionView::Base:0x375eb3c

Extracted source (around line #22):

19:
20:

21:
22: <%= link_to ‘New stocks’, new_stocks_path %>

RAILS_ROOT: K:/_Projects/Ruby/_Rails_Apps/Rails_2.0_Demo
Application Trace | Framework Trace | Full Trace

app/views/stocks/index.html.erb:22:in
_run_erb_47app47views47stocks47index46html46erb' app/controllers/stocks_controller.rb:7:in index’

Hey Richard.

Did you try running rake routes to see if the route exists for
new_stocks?

Post your routes.rb and maybe that will give a clue as to the problem.

Best.
Mike

hmm, I never used scaffolding myself, since
I prefer to put up stuff by hand and know what’s
going on
There where quite some discussions in the last
weeks about scaffolding & Rails 2.x and some stuff
being broken/different to former versions and
all tutorials not working…

anyway: putting that line:

map.resources :stocks

in config/routes.rb should make the thing run

I Thorsten,

Ich bin auch ein “Muller”, but my father dropped the umlaut when he
emigrated to the U.S. perhaps almost a hundred years ago.

map.resources :stocks

Thanks for your excellent ( (very prompt) suggestion. I did that, to
no avail:

K:_Projects\Ruby_Rails_Apps\Rails_2.0_Demo>type config\routes.rb
[snip]

Install the default routes as the lowest priority.

map.resources :stocks # RLM: Addeded 6/21/08
map.connect ‘:controller/:action/:id’
map.connect ‘:controller/:action/:id.:format’
end

I also restarted my WebServer and tried again, but to no avail.

I remain grateful for your response,
Richard Muller

Hi again, Thorsten,

I just discovered that " map.resources :stocks" was already in my
resources.rb file. It was the first line, and so was “hidden” among
all the comments.

I also made a mistake in the salutation: My first line was “I
Thorsten” rather than “Hi Thorsten”.

I apologize for my sloppy mistakes. Perhaps some sloppiness in follow
the tutorial also contributed to my current travails. I hope not.

Best wishes,
Richard Muller

On Jun 21, 2008, at 11:03 AM, RichardOnRails wrote:

There’s no simple “new_stocks” route, but there’s a “new_stock”
Beginners Tutorial: Routing in Rails 2.0 (with REST) – Part 1 of n | Agile Practitioners
POST /stocks
{:action=>“create”, :controller=>“stocks”}
POST /stocks.:format
{:action=>“create”, :controller=>“stocks”}
new_stock GET /stocks/new
{:action=>“new”, :controller=>“stocks”}

change line 22 of app/views/stocks/index.html.erb to match the defined
route:

<%= link_to ‘New stocks’, new_stock_path %>

Hi Mike,

Thank you for your very excellent (and very prompt) response.

Did you try running rake routes to see if the route exists for
new_stocks?

I didn’t even know that “trick”. I posted the result below.

There’s no simple “new_stocks” route, but there’s a “new_stock”
route. That led me to try “http://localhost:3000/stock”, which gave
me:
No route matches “/stock” with {:method=>:get}

I also followed Thorston’s suggestion about adding :
map.resources :stocks

If either you or Thorston have any other ideas, I’d be anxious to try
them out. In the mean time, I’m going to study routing in Rails 2.0
with:

Best wishes,
Richard

K:_Projects\Ruby_Rails_Apps\Rails_2.0_Demo>rake routes
(in K:/_Projects/Ruby/_Rails_Apps/Rails_2.0_Demo)
stocks GET /stocks
{:action=>“index”, :controller=>“stocks”}
formatted_stocks GET /stocks.:format
{:action=>“index”, :controller=>“stocks”}
POST /stocks
{:action=>“create”, :controller=>“stocks”}
POST /stocks.:format
{:action=>“create”, :controller=>“stocks”}
new_stock GET /stocks/new
{:action=>“new”, :controller=>“stocks”}
formatted_new_stock GET /stocks/new.:format
{:action=>“new”, :controller=>“stocks”}
edit_stock GET /stocks/:id/edit
{:action=>“edit”, :controller=>“stocks”}
formatted_edit_stock GET /stocks/:id/edit.:format
{:action=>“edit”, :controller=>“stocks”}
stock GET /stocks/:id
{:action=>“show”, :controller=>“stocks”}
formatted_stock GET /stocks/:id.:format
{:action=>“show”, :controller=>“stocks”}
PUT /stocks/:id
{:action=>“update”, :controller=>“stocks”}
PUT /stocks/:id.:format
{:action=>“update”, :controller=>“stocks”}
DELETE /stocks/:id
{:action=>“destroy”, :controller=>“stocks”}
DELETE /stocks/:id.:format
{:action=>“destroy”, :controller=>“stocks”}
/:controller/:action/:id
/:controller/:action/:id.:format

Michael,

Good news, I think. The tutorial was wrong on only one count (so
far): The model name should be singular, at least for Rails 2.0.2.

With a plural name, your fix allowed my app to start up correctly.
However, if failed when I clicked “new stock”.

When I rebuilt the application with “ruby script/generate scaffold
Stock [snip]”, everything appears to work smoothly.

However, I never would have figured this out without your insight.
So, again, THANK YOU!! BTW, should we report (as a bug) the failure
of the “scaffold” script to flag the erroneous use of a plural model
name?

Best wishes,
Richard

On Jun 21, 2008, at 5:08 PM, RichardOnRails wrote:

Stock [snip]", everything appears to work smoothly.

However, I never would have figured this out without your insight.
So, again, THANK YOU!!

You’re welcome Richard.

BTW, should we report (as a bug) the failure
of the “scaffold” script to flag the erroneous use of a plural model
name?

No, I don’t think you should open a ticket. The “Rails Way” is to name
your models using the singular name.

Best.
Mike

Hey Mike,

Thanks a million!!! I wouldn’t have figured this out by myself for
weeks, if ever, using the 5-part tutorial on Routing.

Now, that you solved the problem, I infer that you compared the error
message,

“Showing stocks/index.html.erb where line #22 raised:
undefined local variable or method `new_stocks_path’ f”

with the output of “rake routes.” Then you observed that a
“new_stock” route was defined, but no “new_stocks” route existed.
From this, you concluded that the “new_stocks_path” should really be
“new_stock_path”.

Is my inference of you reasoning correct?

I further infer that the cause of this problem is a bug in the
scaffold which rake processed with my command:
ruby script/generate scaffold Stocks symbol:string name:string

On the other hand, maybe the tutorial is in error by recommend that a
plural model name be used. I’m leaning in that direction, with a
further guess that the scaffold merely failed to cause the
pluralization error to be flagged.

What do you think?

In any case, thank you VERY MUCH for showing me this solution.

Yours truly,
Richard

On Jun 21, 11:41 am, Michael B. [email protected] wrote:

Did you try running rake routes to see if the route exists for
map.resources :stocks
(in K:/_Projects/Ruby/_Rails_Apps/Rails_2.0_Demo)

{:action=>“edit”, :controller=>“stocks”}
DELETE /stocks/:id

new_stocks?

I opted for:

====================
19:
20:

21:
22: <%= link_to ‘New stocks’, new_stocks_path %>

RAILS_ROOT: K:/_Projects/Ruby/_Rails_Apps/Rails_2.0_Demo
Application Trace | Framework Trace | Full Trace

app/views/stocks/index.html.erb:22:in
_run_erb_47app47views47stocks47index46html46erb' app/controllers/stocks_controller.rb:7:in index’