ruby-on-rails - 动态路径助手 rails

标签 ruby-on-rails ruby ruby-on-rails-3

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/

相关文章:

ruby-on-rails - 区分浏览器请求和来自移动应用程序的请求的最佳方式?

javascript - ajax 调用后在 Rails 中显示通知

ruby-on-rails - rails : get multipart/form-data POST request parameters with same name

ruby - 枚举器作为 Ruby 中的无限生成器

ruby-on-rails - Ruby 中 Association 的相关内容 (on Rails)

ruby-on-rails - 使用 authlogic 登录时调用 current_user 的方法

ruby-on-rails - 如何在重定向时显示 Rails flash 通知?

ruby-on-rails - 如何获取列表中具有相同类别的所有项目

ruby-on-rails - 如何使用 rspec/Rails 5 在 CSV fixture 中编译 ERB?

ruby-on-rails - 回形针弃用方法