rwehresmann
rwehresmann

Reputation: 1178

Custom route redirect to wrong action

I have the following in my routes file:

  resources :exercises, shallow: true do
    resources :questions do
      resources :test_cases do
        member do
          post :run, to: 'test_cases#run'
        end
      end
    end
  end

  get 'test_cases/test', to: 'test_cases#test'

My problem is with the test route, outside the resources. Checking the available routes I have what I want:

test_cases_test GET    /test_cases/test(.:format)                       test_cases#test

However, if I call test_cases_test_url from a view I'm redirected to test_cases#show, no test_cases#test how it should be. This question is about the same problem in a different situation. I don't want to follow the accepted answer because if I put get 'test', to: 'test_cases#test' inside my resources :test_cases and outside member block I'll got the question_id in my route, and inside member block the test_case_id. I don't need these ids.

I have any option to get my desired route(test_cases/test) working?

Upvotes: 0

Views: 202

Answers (1)

Mark Swardstrom
Mark Swardstrom

Reputation: 18110

i think you're matching on the test_cases#show method because it has the same pattern as the test_cases/test url and it comes before. Just move the test_cases/test get route to the top of your routes file

get 'test_cases/test', to: 'test_cases#test'

resources :exercises, shallow: true do
  resources :questions do
    resources :test_cases do
      member do
        post :run, to: 'test_cases#run'
      end
    end
  end
end

Another way, which I'd recommended

resources :exercises, shallow: true do
  resources :questions do
    resources :test_cases do
      member do
        post :run
        get :test
      end
    end
  end
end

Upvotes: 1

Related Questions