gpt4 book ai didi

ruby-on-rails - 带有参数的rails 4路线

转载 作者:数据小太阳 更新时间:2023-10-29 07:24:48 24 4
gpt4 key购买 nike

这是我的路线设置

resources :messages do
collection do
get 'message'
end
end

它工作正常,但我想向这条路线添加一个参数

resources :messages do
collection do
get 'message/:username'
end
end

我在运行 rake routes 时遇到错误

rake aborted!
missing :action
/home/li/data/git/projectname/config/routes.rb:5:in `block (4 levels) in <top (required)>'
/home/li/data/git/projectname/config/routes.rb:4:in `block (3 levels) in <top (required)>'
/home/li/data/git/projectname/config/routes.rb:3:in `block (2 levels) in <top (required)>'
/home/li/data/git/projectname/config/routes.rb:2:in `block in <top (required)>'
/home/li/data/git/projectname/config/routes.rb:1:in `<top (required)>'
/home/li/data/git/projectname/config/environment.rb:5:in `<top (required)>'
Tasks: TOP => routes => environment
(See full trace by running task with --trace)

向该路由添加参数的正确方法是什么?

最佳答案

应该是

resources :messages do
collection do
get 'message/:username' => :message
end
end

如果您想使用 messages_message_urlmessages_message_path,请使用带有 as: 选项的命名路由:

resources :messages do
collection do
get 'message/:username' => :message, as: 'messages_message'
end
end

关于ruby-on-rails - 带有参数的rails 4路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20818447/

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