gpt4 book ai didi

ruby-on-rails - 在 Rails 路由中定义 url 助手

转载 作者:行者123 更新时间:2023-12-02 17:04:34 25 4
gpt4 key购买 nike

之前不需要使用 rails 的“url helpers”,但我正在尝试实现类似 Rails: URL/path with parameters 的东西.我没有创建任何“资源”,但我的印象是我可以在路由中添加 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'

(注意“get”小写,您在第一个链接上看到的是命令 rake routes 的输出,而不是您定义它们时的行)

该路由将为您提供 2 个要使用的命名路由:users_index_pathusers_index_url(后者包括协议(protocol)、主机和端口)。

关于ruby-on-rails - 在 Rails 路由中定义 url 助手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52397106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com