0

在 Rails 中,当您使用脚手架创建模型时,如下所示:

rails 生成脚手架用户名:字符串电子邮件:字符串

它将生成:模型、控制器和视图

例如,在 index.html.erb 文件中的视图有一个列出所有用户注册的表格。每个用户都有链接:显示、编辑、销毁

在 index.html.erb 中,这些由以下几行表示:

 <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>

还有一个新用户链接,表示为:

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

但是,如果我在没有脚手架的情况下手动创建模型、视图和控制器,则不会生成这些“路径”。“路径”是指:new_student_path、edit_student_path(student)、student

如何手动生成这些?

4

2 回答 2

1

当您将资源添加到 config/routes.rb路径时,将自动生成。

假设您添加了一个StudentsController手动命名的控制器。要获取 new_student_path、edit_student_path 等,您需要将此行添加到config/routes.rb

resources :students

这增加了七种宁静动作的路径。您可以在此 url 上阅读有关 rails 路由的更多信息:http: //guides.rubyonrails.org/routing.html

于 2012-04-16T13:02:55.020 回答
0

编辑您的config/routes.rb. 您可以添加例如:

resources :students

您可以在http://guides.rubyonrails.org/routing.html看到更多信息

您可以在命令行中键入 rake 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
于 2012-04-16T13:03:44.387 回答