Forum: Ruby on Rails scaffold and links created

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5941ffd1e07a9d29499ffd033d46f6ba?d=identicon&s=25 bandejapaisa (Guest)
on 2009-05-17 22:47
(Received via mailing list)
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: <br />
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
1e7782e67bb34c9c67ed19d5cde5f4eb?d=identicon&s=25 Tom Z Meinlschmidt (Guest)
on 2009-05-18 14:16
(Received via mailing list)
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
===============================================================================
D188e591eac11021329b8821a5f954c7?d=identicon&s=25 Ar Chron (railsdog)
on 2009-05-18 15:41
rake routes >routes.lst  will write all the current route definitions to
a file for you to view.
Af2ce6689213fdb78913a9662b18da6b?d=identicon&s=25 Rick Lloyd (ricklloyd)
on 2009-05-18 20:30
(Received via mailing list)
You may need to stop and restart your server (mongrel? webrick?) after
running the scaffold command to reload the new routes associated with
User.
687ec1a3547cae86723a85ed4e8456d9?d=identicon&s=25 Sak Gz (sak)
on 2009-06-11 06:07
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
D188e591eac11021329b8821a5f954c7?d=identicon&s=25 Ar Chron (railsdog)
on 2009-06-11 16:00
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/ActionControlle...

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}
This topic is locked and can not be replied to.