user984621
user984621

Reputation: 48453

Match of routes in the collection

I define each actions in my controller this way (in routes.rb):

  resources :home do
    collection do
      get "home/index"
      get "home/about_me"
      get "home/contact"
    end
  end

If I would use a match for the action about_me, I have to use

      resources :home do
        collection do
          get "home/index"
          get "home/about_me"
          get "home/contact"
        end
      end

      match 'about-me' => 'home#about_me'

Exist any way, how to add match rule direct into a collection? I mean something like this:

  resources :home do
    collection do
      get "home/index"
      get "home/about_me", match => "about-me"
      get "home/contact"
    end
  end

And I have one question yet - when I use in the routes.rb the second block of code, so when I set the URL address about-me, so the address works fine, but when I type there home/about_me, so I get the error

Unknown action: The action 'show' could not be found for HomeController.

What caused this error?

Upvotes: 1

Views: 1372

Answers (1)

Ashitaka
Ashitaka

Reputation: 19203

I think the problem here is that your routes have the home/ prefix when they are nested inside resources :home. Try this:

 resources :home do
   collection do
     get :index
     get :about_me
     get :contact
   end
 end

Also, when you have trouble setting your routes, type rake routes in your console. This will generate the routes of your app and the corresponding paths and controllers.

EDIT: Here's the answer to your other question.

resources :home do
  collection do
    get 'about_me' => 'about-me'
  end
end

Upvotes: 3

Related Questions