AConsumer
AConsumer

Reputation: 2781

Rails hitting wrong url

I made GET request with the following url in postman

localhost:8091/trade/search?side=SELL

Expected behavior

Rails should have triggered searchTrade action get '/trade/search/:commodity/:side/:counterparty/:location' => 'tradeservice#searchTrade'

Actual behavior

Rails misinterpreted the route as: match '/trade/:id', to: 'tradeservice#getTradeById', via: :get

Logs

image

System configuration

Rails version: 5.1.7 Ruby version: 2.3.3.p222

This is definitely a bug, how do i fix this ??

Upvotes: 0

Views: 186

Answers (1)

Timur Nugmanov
Timur Nugmanov

Reputation: 893

Rails routes are matched in the order they are specified (source).
/trade/search matches both /trade/search and /trade/:id since :id is a param.

The fix would be to place

get '/trade/search/:commodity/:side/:counterparty/:location' => 'tradeservice#searchTrade'

above

match '/trade/:id', to: 'tradeservice#getTradeById', via: :get

You might also want to make your endpoint look like this:

get '/trade/search' => 'tradeservice#searchTrade'

since you pass the params in the query string (?side=SELL) rather than in the path.

Upvotes: 3

Related Questions