[英]Dynamic path helpers rails
What are the paths that is automatically added by Rails? Rails自动添加的路径是什么? Let say you have a Question resource you automatically get questions_path, question_path etc. Where do I see what they resolve to and what I get?
假设您有一个问题资源,您自动获得questions_path,question_path等。我在哪里可以看到他们解决了什么以及我得到了什么?
This section might be helpful http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use 本节可能会有所帮助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)
If you want to create a helper for show
action you can write 如果你想为
show
action创建一个帮助器,你可以写
photo_path(@photo.id)
where @photo
is your model object. 其中
@photo
是你的模型对象。 Or you can pass @photo
directly if it responds to id
method. 或者你可以直接传递
@photo
,如果它响应id
方法。
photo_path(@photo)
edit_photo_path(@photo)
You can also load rails console
(in terminal) and test routes using app
like so app.photo_path(1)
(it will show you the route for the photo with id
equals 1
) 您还可以使用
app
app.photo_path(1)
加载rails console
(在终端中)和测试路径(它将显示id
等于1
的照片的路径)
Just use: 只需使用:
rake routes
This will list all routes defined. 这将列出定义的所有路线。 The first column is relevant for you path helpers.
第一列与您的路径助手相关。
If you have the following in your routes file: 如果路径文件中包含以下内容:
resources :questions
Then Rails provides the following restful routes for you: 然后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
You can also run rake:routes to see what is being generated. 您还可以运行rake:routes来查看正在生成的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.