Here is my route settings
resources :messages do
  collection do
    get 'message'
  end      
end
It works fine but I would like to add a parameter to this route
resources :messages do
  collection do
    get 'message/:username'
  end      
end
I got an error when I ran 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)
What is the right way to add a parameter to this route?
It should be
resources :messages do
  collection do
    get 'message/:username' => :message
  end      
end
If you want to use messages_message_url and messages_message_path, use a named route with the as: option:
resources :messages do
  collection do
    get 'message/:username' => :message, as: 'messages_message'
  end      
end
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With