Reputation: 5482
Let's start off with some code:
Rails.application.routes.draw do
namespace :mpd do
get 'help' #==> get "mpd/help", as: :mpd_help
get 'status'
post 'start'
post 'stop'
post 'next'
post 'previous'
post 'pause'
post 'update'
# post 'play_song/:id', to: 'mpd#play_song'
end
# For some reason this path must not be in the namespace?!
post '/mpd/play_song/:id', to: 'mpd#play_song', as: 'mpd_play_song'
root 'static#home'
#match '*path' => 'static#home', via: [:get, :post]
end
Why do I have to specify the mpd_play_song_path outside of my namespace? It uses the same controller and a function within, however, I receive the following error upon putting it within the namespace:
undefined method `mpd_play_song_path' for #<#<Class:0x007f2b30f7fd20>:0x007f2b30f7eb50>
And this is the line within my view:
= link_to("Play", mpd_play_song_path(song[:id])
I find this fairly strange and do not see any reason besides the passed id
why it shouldn't work.
Hit me up if you need more code. Thanks in advance,
Phil
Upvotes: 1
Views: 146
Reputation: 76774
Namespace
Having a namespace does not denote the controller your routes will assume.
A namespace is basically a folder within which you'll place controllers.
You still have to use the resources
directive and set the controller actions:
#config/routes.rb
namespace :mdp do
resources :controller do
collection do
get :help #-> url.com/mdp/controller/help
end
end
end
It seems to me that you're wanting to use the mdp
controller, which means you'd set up your routes as follows:
#config/routes.rb
resources :mdp do
get :help, action: :show, type: :help
get :status, action: :show, type: :status
...
end
A more succinct way will be to use constraints:
#config/routes.rb
resources :mdp, except: :show do
get :id, to: :show, constraints: ActionsConstraints
end
#lib/actions_constraints.rb
class NewUserConstraint
def self.matches?(request)
actions = %i(help status)
actions.include? request.query_parameters['id']
end
end
Upvotes: 1