2012-08-08 50 views
18

Rails自动添加的路径是什么?假设你有一个问题资源,你会自动获得questions_path,question_path等。我在哪里可以看到他们的解决方案和我得到的结果?动态路径助手栏杆

回答

34

如果你想创建show行动帮助这部分可能会有所帮助http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use

Verb Path    Action  Helper 

GET  /photos   index  photos_path 
GET  /photos/new  new  new_photo_path 
POST /photos   create  photos_path 
GET  /photos/:id  show  photo_path(:id) 
GET  /photos/:id/edit edit  edit_photo_path(:id) 
PUT  /photos/:id  update  photo_path(:id) 
DELETE /photos/:id  destroy photo_path(:id) 

你可以写

photo_path(@photo.id) 

其中@photo是你的模型对象。或者,如果它响应id方法,则可以直接通过@photo

photo_path(@photo) 
edit_photo_path(@photo) 

您也可以加载rails console(端),以及使用app像测试路由,以便app.photo_path(1)(它会告诉你用id照片的路线等于1

8

只需使用:

rake routes 

这将列出定义的所有路线。第一列与你的路径助手相关。

+0

[here](http://guides.rubyonrails.org/routing.html#seeing-existing-routes-with-rake)是什么'rake routes'给你的详细解释 – 2012-08-08 11:27:12

+0

我看不到这是怎么回事显示我可以作为参数传递给帮手等? – LuckyLuke 2012-08-08 12:22:57

+0

@Dude,看我的更新 – evfwcqcg 2012-08-08 13:46:08

0

如果您在在下面的routes文件:

resources :questions 

然后Rails提供以下宁静的路线你:

GET  /questions   index  list of questions 
GET  /questions/new  new   show new question form 
POST /questions   create  create a new question 
GET  /questions/:id  show  show a specific question 
GET  /questions/:id/edit edit  show form to edit question 
PUT  /questions/:id  update  update a specific question 
DELETE /questions/:id  destroy  delete a specific question 

您还可以运行rake:routes来查看正在生成的内容。