Search code examples
ruby-on-railsrubyurlroutesrails-routing

Rails routes config thinks action method is an object id


I'm developing rails application and encountered such problem.

I have movies_controller.rb, where I have these actions and routes defined:

            Prefix Verb   URI Pattern                   Controller#Action
            movies GET    /movies(.:format)             movies#index
                   POST   /movies(.:format)             movies#create
         new_movie GET    /movies/new(.:format)         movies#new
        edit_movie GET    /movies/:id/edit(.:format)    movies#edit
             movie GET    /movies/:id(.:format)         movies#show
                   PATCH  /movies/:id(.:format)         movies#update
                   PUT    /movies/:id(.:format)         movies#update
                   DELETE /movies/:id(.:format)         movies#destroy
              root GET    /                             redirect(301, /movies)
movies_by_director GET    /movies/by_director(.:format) movies#by_director

But when I try to go to /movies/by_director?director="something", rails think, that I'm navigating to movies#show action with parameter :id = by_director.

What am I doing wrong?


Solution

  • Routes are matched in the order they are specified so make sure the route for "by_director" is defined above the resource routes for movies.

    Something like this should do the trick:

    get '/movies/by_director' => 'movies#by_director'
    resources :movies