bytebiscuit
bytebiscuit

Reputation: 3496

add new_foo_path manually in rails

In Rails, when you create a model using scaffold like the following:

rails generate scaffold User name:string email:string

It will generate the: models, controllers, and views

The views for example, in the index.html.erb file there is table which lists all the Users registers. For each user there are links: Show, Edit, Destroy

in the index.html.erb these are represented by the following lines:

 <td><%= link_to 'Show', student %></td>
 <td><%= link_to 'Edit', edit_student_path(student) %></td>
 <td><%= link_to 'Destroy', student, :confirm => 'Are you sure?', :method => :delete %></td>

and there also is a New User link which is represented by:

<%= link_to 'New Student', new_student_path %>

However, if I create a model,view and controller manually without the scaffold, then these 'paths' won't be generated. By 'paths' I mean: new_student_path, edit_student_path(student), student

How do I generate these manually?

Upvotes: 0

Views: 395

Answers (2)

Michael Durrant
Michael Durrant

Reputation: 96484

Edit your config/routes.rb. You can add e.g. this:

resources :students

You can see more this at http://guides.rubyonrails.org/routing.html

You can type rake routes at the command line to see what routes are available before and after doing this.
Basically you will get the following routes:

HTTP VerbPath   action  used for 
GET     /photos         index   display a list of all photos 
GET     /photos/new     new     return an HTML form for creating a new photo 
POST    /photos         create  create a new photo 
GET     /photos/:id     show    display a specific photo 
GET     /photos/:id/edit edit   return an HTML form for editing a photo 
PUT     /photos/:id     update  update a specific photo 
DELETE  /photos/:id     destroy delete a specific photo

Upvotes: 0

Andreas Heim
Andreas Heim

Reputation: 581

When you add the resources to config/routes.rb the paths will get generated automatically.

Let's say that you have added a controller named StudentsController manually. To get new_student_path, edit_student_path etc. you need to add this line to config/routes.rb

resources :students

This adds the paths for the seven restful actions. You can read more on the rails routing on this url: http://guides.rubyonrails.org/routing.html

Upvotes: 1

Related Questions