Skip to content

Routes inside constraints block don't respect format constraints #94

@waratuman

Description

@waratuman

Problem

When wrapping standard_resources in a constraints format: :json block, the format constraint is not enforced. Requests without a .json extension (e.g. GET /transactions) still match the route and hit the controller, resulting in a StandardAPI::ParameterMissing error because required params like limit are missing.

# Expected: /transactions should NOT match, /transactions.json should match
constraints format: :json do
  standard_resources :transactions
end

Workaround

Using a lambda constraint works:

constraints(->(req) { req.format.json? }) do
  standard_resources :transactions
end

Expected Behavior

constraints format: :json should restrict standard_resources routes to only match when the format segment is json, consistent with how resources behaves when wrapped in the same block.

Context

This matters for SPAs where bare paths like /transactions should fall through to a catch-all route serving the frontend, while /transactions.json should hit the API controller.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions