动态路径助手轨道
Rails自动添加的路径是什么? 假设您有一个问题资源,您自动获得questions_path,question_path等。我在哪里可以看到他们解决的问题以及我得到了什么?
本节可能会有所帮助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)
如果您想为show
动作创建一个帮助器,您可以编写
photo_path(@photo.id)
其中@photo
是你的模型对象。 或者你可以直接传递@photo
,如果它响应id
方法。
photo_path(@photo) edit_photo_path(@photo)
你也可以使用app
app.photo_path(1)
加载rails console
(在终端中)和测试路由(它会显示id
等于1
的照片路径)
只需使用:
rake 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来查看正在生成的内容。