Search code examples
ruby-on-railsrubyrack-test

Rack::Test::Methods setting id when using get method


I'm struggling with API methods from Rack::Test. Providing the resource parametets, makes sense for the post method, but for get, seems, it doesn't. When I want to fetch the resource with definite id, I'm trying to make the request:

get "/resources/#{id}", {Accept: Mime::JSON}

and it actually works, but it doesn't, if I provide an id as a second argument:

get '/resources', id: id, {Accept: Mime::JSON} 
# or
get '/resources', {id: id}, {Accept: Mime::JSON}
# omits the id - just fetches all the resources

How do I provide the id as a separate parameter? Embedding it inside a string looks awful.


Solution

  • The seconds parameter identifies the payload of the request. GET doesn't have payload, since you can't send any data when you perform a GET request.

    In a get request, the key/value payload is represented by the query string (plus the URI), therefore the first method is the way to go.

    If instead of GET you use a different method such as POST, PUT, DELETE or PATCH, then the second parameter represents the payload of the request.

    patch "/resources/#{id}", { foo: "bar" }.to_json, {Accept: Mime::JSON}