Can't get a simple application to work with Rails 2.0

Clearly I’m doing something wrong, but should it really be this
difficult to figure out?

I found the Rails 2.0 tutorial movie and did the following:

rails ESPlanner_Web --d=mysql

Edited database.yml appropriately

script/generate scaffold users Username:string Password:String

Tweaked the migrations to get the lengths right and added an index.

rake db:migrate

Checked via phpmyadmin that the tables got set up properly (they did).

Modified routes.rb to start at the users controller.
Deleted public/index.html.

script/server

Hit localhost:3000 using Firefox and get:

NameError in Users#index

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

undefined local variable or method `new_users_path’ for
#ActionView::Base:0x25013cc

Extracted source (around line #22):

19:
20:

21:
22: <%= link_to ‘New users’, new_users_path %>

By which I assume that the index method of the Users controller didn’t
define new_users_path so I looked at that and see:

def index
@users = Users.find(:all)

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @users }
end

end

presumably the format.html statement is magic that hits index.html.erb
which requires new_users_path to be defined but it isn’t.

Am I doing something wrong or is the scaffold generator broken?

Best,

Dick Munroe

The problem is in your routes.rb file. Somewhere, you have nullified
this
command:

map.resources :users

I can’t tell without seeing your routes file, but you will want to
ensure
that that line exists in that file. Feel free to paste your entire
routes.rb file here so we can look at it. The new_users_path method is
defined by that single line in routes.rb.

Also, to save you possible trouble in the future, table names and
column
names are not uppercased. Uppercased things in Ruby mean that they are
Constants! (I noticed your scaffold command had the field names with
capital
letters so that’s why I brought it up.)

Well, the routes file (with the exception of the map.root is generated
by rails. Here it is:

ActionController::Routing::Routes.draw do |map|
map.resources :families

map.resources :users

The priority is based upon order of creation: first created ->

highest priority.

Sample of regular route:

map.connect ‘products/:id’, :controller => ‘catalog’, :action =>

‘view’

Keep in mind you can assign values other than :controller

and :action

Sample of named route:

map.purchase ‘products/:id/purchase’, :controller =>

‘catalog’, :action => ‘purchase’

This route can be invoked with purchase_url(:id => product.id)

Sample resource route with options:

map.resources :products, :member => { :short => :get, :toggle

=> :post }, :collection => { :sold => :get }

Sample resource route with sub-resources:

map.resources :products, :has_many =>

[ :comments, :sales ], :has_one => :seller

Sample resource route within a namespace:

map.namespace :admin do |admin|

# Directs /admin/products/* to Admin::ProductsController (app/

controllers/admin/products_controller.rb)

admin.resources :products

end

You can have the root of your site routed with map.root – just

remember to delete public/index.html.
map.root :controller => “users”

See how all your routes lay out with “rake routes”

Install the default routes as the lowest priority.

map.connect ‘:controller/:action/:id’
map.connect ‘:controller/:action/:id.:format’
end

When I do a rake routes I get:

         families GET    /families

{:controller=>“families”, :action=>“index”}
formatted_families GET /families.:format
{:controller=>“families”, :action=>“index”}
POST /families
{:controller=>“families”, :action=>“create”}
POST /families.:format
{:controller=>“families”, :action=>“create”}
new_family GET /families/new
{:controller=>“families”, :action=>“new”}
formatted_new_family GET /families/new.:format
{:controller=>“families”, :action=>“new”}
edit_family GET /families/:id/edit
{:controller=>“families”, :action=>“edit”}
formatted_edit_family GET /families/:id/edit.:format
{:controller=>“families”, :action=>“edit”}
family GET /families/:id
{:controller=>“families”, :action=>“show”}
formatted_family GET /families/:id.:format
{:controller=>“families”, :action=>“show”}
PUT /families/:id
{:controller=>“families”, :action=>“update”}
PUT /families/:id.:format
{:controller=>“families”, :action=>“update”}
DELETE /families/:id
{:controller=>“families”, :action=>“destroy”}
DELETE /families/:id.:format
{:controller=>“families”, :action=>“destroy”}
users GET /users
{:controller=>“users”, :action=>“index”}
formatted_users GET /users.:format
{:controller=>“users”, :action=>“index”}
POST /users
{:controller=>“users”, :action=>“create”}
POST /users.:format
{:controller=>“users”, :action=>“create”}
new_user GET /users/new
{:controller=>“users”, :action=>“new”}
formatted_new_user GET /users/new.:format
{:controller=>“users”, :action=>“new”}
edit_user GET /users/:id/edit
{:controller=>“users”, :action=>“edit”}
formatted_edit_user GET /users/:id/edit.:format
{:controller=>“users”, :action=>“edit”}
user GET /users/:id
{:controller=>“users”, :action=>“show”}
formatted_user GET /users/:id.:format
{:controller=>“users”, :action=>“show”}
PUT /users/:id
{:controller=>“users”, :action=>“update”}
PUT /users/:id.:format
{:controller=>“users”, :action=>“update”}
DELETE /users/:id
{:controller=>“users”, :action=>“destroy”}
DELETE /users/:id.:format
{:controller=>“users”, :action=>“destroy”}
root /
{:controller=>“users”, :action=>“index”}
/:controller/:action/:id
/:controller/:action/:id.:format

Boy, I wish there was some documentation of what’s going on here…

Thanks for any help

Dick

:slight_smile: No. The helpers you’re looking for are defined automatically by the
map.resources stuff in routes.rb, which you yourself verified in the
rake
task.

Change

new_users_path

to

new_user_path

That will work. (note the singular form… didn’t notice that myself
until I
reread your original message.) Now, I can’t believe that the generator
would get that wrong. Did you modify it by any chance?

If you’re sure you didn’t, try creating a brand new app doing the same
thing. It should just work.

And that appears to be at least part of the problem. When I define a
helper of the form:

def new_users_path
“/users/new”
end

at least I can get the page displayed.

Is this a bug in rails scaffolding? or did I do something wrong when I
generated the scaffold?

Best,

Dick Munroe

After doing a little digging in the Agile Web D. book, it
looks like the helpers for the views aren’t being defined. I looked
in app/helpers/users_helper.rb and find:

module UsersHelper
end

Should rails have defined a bunch of helpers here? or does that get
done dynamically by the map.resources in routes.db?

Best,

Dick Munroe

Yeah, it turns out that using the plural form when I generated the
scaffold was the problem. I didn’t see anything in the Agile
Development book about plurals versus non-plurals so that got me.

Lots of under the hood stuff going on here.

Thanks for the help

Dick Munroe

The book uses the rails 1.x version of scaffolding. IF you have Rails
2.0installed then there’s going to be a LOT of stuff that doesn’t work
for you.

I don’t believe there is. I just generated a new app from scratch and
all of
the pages were created as expected. new_users_path is not a valid route
name. new_user_path is.

How did you geneerate your scaffold?

Correct command should be ruby script/generate user username:string
password:string

If you did ruby script/generate users (note the plural) I could see that
happening.

You most certainly do NOT need to have your own helper.

I just ran into that same problem myself. The problem stems from the
fact that you are manually pluralizing “users”.

The line that is causing the problem is:

script/generate scaffold users Username:string Password:String

instead use the word “user”

script/generate scaffold user Username:string Password:String

I think Rails is getting confused with in its pluralization somewhere.

Hope this helps.

[email protected] wrote:

Clearly I’m doing something wrong, but should it really be this
difficult to figure out?

I found the Rails 2.0 tutorial movie and did the following:

rails ESPlanner_Web --d=mysql

Edited database.yml appropriately

script/generate scaffold users Username:string Password:String

Tweaked the migrations to get the lengths right and added an index.

rake db:migrate

Checked via phpmyadmin that the tables got set up properly (they did).

Modified routes.rb to start at the users controller.
Deleted public/index.html.

script/server

Hit localhost:3000 using Firefox and get:

NameError in Users#index

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

undefined local variable or method `new_users_path’ for
#ActionView::Base:0x25013cc

Extracted source (around line #22):

19:
20:

21:
22: <%= link_to ‘New users’, new_users_path %>

By which I assume that the index method of the Users controller didn’t
define new_users_path so I looked at that and see:

def index
@users = Users.find(:all)

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @users }
end

end

presumably the format.html statement is magic that hits index.html.erb
which requires new_users_path to be defined but it isn’t.

Am I doing something wrong or is the scaffold generator broken?

Best,

Dick Munroe

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs