Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails 4带参数的路由_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails rails 4带参数的路由

Ruby on rails rails 4带参数的路由,ruby-on-rails,ruby,Ruby On Rails,Ruby,这是我的路线设置 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/gi

这是我的路线设置

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)
rake中止!
缺失:行动
/home/li/data/git/projectname/config/routes.rb:5:in'block(4层)in'
/home/li/data/git/projectname/config/routes.rb:4:in'block(3层)in'
/home/li/data/git/projectname/config/routes.rb:3:in'block(2层)in'
/home/li/data/git/projectname/config/routes.rb:2:in'block in'
/home/li/data/git/projectname/config/routes.rb:1:in`'
/home/li/data/git/projectname/config/environment.rb:5:in`'
任务:TOP=>routes=>environment
(通过使用--trace运行任务查看完整跟踪)
向该路由添加参数的正确方法是什么?

应该是

resources :messages do
  collection do
    get 'message/:username' => :message
  end      
end
如果要使用
消息\u消息\u url
消息\u消息\u路径
,请使用带有
as:
选项的命名路由:

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

为什么
get'message/:username'=>'message'
不起作用?@wwli我不确定,我认为
get'message'
get'message'=>'message'
的快捷方式,它不能与动态段一起工作。很抱歉再次打扰你,我试图运行
rake-routes
为什么前缀是空的。我试图使用
messages\u messages\u path
我遇到了一个错误
undefined method
messages\u path`这个路由缺少一些设置?