Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-3.1

Complex routing issues Rails 3


I need to match complex routes which is causing mismatch, These are my routes file entries.

  match 'households/:id/edit'=>'households#edit', :via=>:get, :as=>:edit_household, :id=>/\d{1,5}/
  match 'households/:id' => 'households#update', :via => :put
  match 'households/:id' => 'households#destroy', :via=>:delete
  match 'households/:city_code-:region_code'=>'households#index', :via=>:get, :as=>:households, :city_code=>/[A-Z]{3,5}/, :region_code=>/[A-Z]{0,3}\d{0,5}/
  match 'households/:city_code-:region_code-:h_uid'=>'households#show', :via=>:get, :as=>:household, :city_code=>/[A-Z]{3,5}/, :region_code=>/[A-Z]{0,3}\d{0,5}/

Now the problem is that the request

http://0.0.0.0:3000/households/9/edit

is routing to the show controller and the error is generating

Routing Error

No route matches {:controller=>"households", :action=>"show", :format=>nil, :city_code=>#<Household id: 9, uid: "0004", house_no: "House No 12", street: "Street 512", address: "Lorem ipsum dolor sit amet, consectetur adipisicing...", region_id: 3, created_at: "2012-01-05 11:06:32", updated_at: "2012-01-05 11:06:32">}

Solution

  • I changed my routes to:

      resources :households, :only=>[:index, :edit, :update, :destroy] do
        collection do
          match ':city_code-:region_code-:h_uid'=>'households#show', :via=>:get, :as=>:show, :city_code=>/[A-Z]{3,5}/, :region_code=>/[A-Z]{0,3}\d{0,5}/
        end
      end
    

    and every thing is working fine.