Rails 自动添加的路径是什么?假设您有一个 Question 资源,您会自动获得 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
Action 创建一个助手,你可以这样写
photo_path(@photo.id)
其中 @photo
是您的模型对象。或者你可以直接传递 @photo
如果它响应 id
方法。
photo_path(@photo)
edit_photo_path(@photo)
您还可以加载 rails console
(在终端中)并使用 app
测试路由,就像这样 app.photo_path(1)
(它将显示 id
等于 1
的照片的路线)
关于ruby-on-rails - 动态路径助手 rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11863305/