Scaffold and links created


#1

I used scaffold command

 generate scaffold User username:string password:string

email:string validation_code:string validated:boolean

and when i go to http://localhost:3000/users/ i get this error:

undefined local variable or method `new_user_path’ for
#ActionView::Base:0x5f1c3f4

25:
26:

27:
28: <%= link_to ‘New user’, new_user_path %>

Where is the local variable new_user_path supposed to be defined (the
controller?)… and why didn’t the script generate this?

Thanks

Ruby version 1.8.6 (i386-mswin32)
RubyGems version 1.3.3
Rails version 2.2.2
Active Record version 2.2.2
Action Pack version 2.2.2
Active Resource version 2.2.2
Action Mailer version 2.2.2
Active Support version 2.2.2


#2

add
map.resources :users

to your config/routes.rb file

tom

bandejapaisa wrote:

25:
Ruby version 1.8.6 (i386-mswin32)
RubyGems version 1.3.3
Rails version 2.2.2
Active Record version 2.2.2
Action Pack version 2.2.2
Active Resource version 2.2.2
Action Mailer version 2.2.2
Active Support version 2.2.2

Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache

  • experienced RoR/PHP freelancer, available for hire

www.meinlschmidt.com www.maxwellrender.cz www.lightgems.cz


#3

rake routes >routes.lst will write all the current route definitions to
a file for you to view.


#4

You may need to stop and restart your server (mongrel? webrick?) after
running the scaffold command to reload the new routes associated with
User.


#5

Hi all,

I have the same problem, after create the scaffold I try to do new
methods in the controller and when I want to call it, I see

undefined local variable or method `semana_post_path’ for
#ActionView::Base:0xb610e4cc

Then I try changing the url http://localhost:3000/posts/semana

Couldn’t find Post with ID=semana

app/controllers/posts_controller.rb:44:in `show’

I try to restart the server (webrick), map.resources is active too and
the routes.lst is this:

$ more routes.lst
(in /home/sak/Projectos/Rails/vigg2)
posts GET /posts
{:controller=>"posts
", :action=>“index”}
formatted_posts GET /posts.:format
{:controller=>"posts
", :action=>“index”}
POST /posts
{:controller=>"posts
", :action=>“create”}
POST /posts.:format
{:controller=>"posts
", :action=>“create”}
new_post GET /posts/new
{:controller=>"posts
", :action=>“new”}
formatted_new_post GET /posts/new.:format
{:controller=>"posts
", :action=>“new”}
edit_post GET /posts/:id/edit
{:controller=>"posts
", :action=>“edit”}
formatted_edit_post GET /posts/:id/edit.:format
{:controller=>"posts
", :action=>“edit”}
post GET /posts/:id
{:controller=>"posts
", :action=>“show”}
formatted_post GET /posts/:id.:format
{:controller=>"posts
", :action=>“show”}
PUT /posts/:id
{:controller=>"posts
", :action=>“update”}
PUT /posts/:id.:format
{:controller=>"posts
", :action=>“update”}
DELETE /posts/:id
{:controller=>"posts
", :action=>“destroy”}
DELETE /posts/:id.:format
{:controller=>"posts
", :action=>“destroy”}
root /
{:action=>“index”, :
controller=>“posts”}
/:controller/:action/:id
/:controller/:action/:id.:format

Any idea why rails ignore the new methods in the controller? It’s very
important for me.
Thanks


#6

Rails is ignoring your new method in the controller because you haven’t
told Rails about it in your routes.rb file.

If you add a method in the controller that is the target of an HTTP
request, you’ll need to add that method to the routes.rb file.

http://api.rubyonrails.org/classes/ActionController/Resources.html

Where you probably have:

map.resources :posts

you need to tell Rails about any added methods.

Added methods for a collection of posts goes into a :collection =>
{:action => :verb} hash.
Added methods for a single post goes into a :member => {:action =>
:verb} hash.

:action is your controller method name,
:verb is the HTTP verb, :get, :post, :put, :delete (or :any)

If you’ve implemented a ‘remove_all’ method for the collection of posts,
as well as a generate_pdf method for a single post, your routes.rb might
look like:

map.resources :posts, :collection => {:remove_all => :delete}, :member
=> {:generate_pdf => :get}