Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails 4 route with parameter

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?

like image 462
wwli Avatar asked Dec 26 '22 15:12

wwli


1 Answers

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
like image 172
Baldrick Avatar answered Dec 28 '22 08:12

Baldrick