use*_*446 2 ruby-on-rails ruby-on-rails-5
以前不需要使用 Rails 'url helpers' ,但我正在尝试实现类似Rails: URL/path withparameters的东西。我没有创建任何“资源”,但我的印象是我可以在路由中添加 url_helper 名称,例如:
user_index_path GET 'users/index', to: 'users#index'
但这给出了错误:
undefined method 'GET' for #<ActionDispatch::Routing::Mapper:0x00000007ABCDEF> Did you mean? gets gem
由于我没有太多使用它们,我也对我在这里读到的声明感到困惑,https://blog.arkency.com/all-the-ways-to-generate-routing-paths-in-rails/,说“当然有时你需要 _url 而不是 _path”。我对它们的定义是错误的吗?是的,我阅读了https://guides.rubyonrails.org/routing.html,并看到了有关“3.16 直达路线”的内容,但这与我“看到”的示例并不相符。
您可以随意命名路线
#inside your routes.rb file
get 'users/index', to: 'users#index', as: 'users_index'
Run Code Online (Sandbox Code Playgroud)
(注意“get”小写,您在第一个链接上看到的是命令的输出rake routes,而不是定义它们时的行)
该路由将为您提供 2 个可供使用的命名路由:users_index_path和users_index_url(后者包括协议、主机和端口)。
| 归档时间: |
|
| 查看次数: |
2272 次 |
| 最近记录: |