开发者

How to avoid resolving a route in rails?

开发者 https://www.devze.com 2023-03-13 04:38 出处:网络
I\'m developing a rails app with an admin section and a public section. In the public section I want to resolve only index and show routes

I'm developing a rails app with an admin section and a public section. In the public section I want to resolve only index and show routes

resources :services, :only => [:index, :show]

However, when I hit the standard URL for the new action it resolves to the show action with an id of 'new'. That is http://foo.com/services/new returns an error "Couldn't find Service with ID=new". Is there anyway I can tell rails NOT to resolve /services/new?

I've alrea开发者_如何学Cdy tried

resources :services, :only => [:index, :show], :except => :new

and

resources :services, :except => :new, :only => [:index, :show]

without success.

ETA (by request): My entire routes file (sans comments):

MyApp::Application.routes.draw do
  resources :services, :only => [:index, :show]
  resources :packages,:only => [:index, :show]

  get "pages/home"
  get "pages/about"
  get "pages/help"

  root :to => 'packages#index'

  namespace "admin" do
    get "pages/home"
    get "pages/about"
    get "pages/help"

    resources :services
    match "/services/:id/add_to_package/:package_id" => "services#add_package",       :as => :add_package_to_service,      :via => :post, :id => /\d+/, :package_id => /\d+/
    match "/services/:id/remove_from_package/:package_id" => "services#remove_package", :as => :remove_package_from_service, :via => :post, :id => /\d+/, :package_id => /\d+/

    resources :packages 
    match "/packages/:id/add_service/:service_id" => "packages#add_service",       :as => :add_service_to_package,      :via => :post, :id => /\d+/, :service_id => /\d+/
    match "/packages/:id/remove_service/:service_id" => "packages#remove_service", :as => :remove_service_from_package, :via => :post, :id => /\d+/, :service_id => /\d+/

    resources :users

    root :to => 'pages#home'
  end
end


You can try to putting a constraint on your :id param

resources :services, :constraints => {:id => /\d+/}, :only => [:index, :show]

This is assuming your :ids are all number based.

I had a similar situation with a redirect vs resource collision, this fixed it.

0

精彩评论

暂无评论...
验证码 换一张
取 消