Skip to content

Links broken when used with scope module: routes #591

Open
@mitnal

Description

@mitnal

Hi,

we use a version parameter inside of the Accept header to be able to switch Api versions. The different versions are grouped in modules. And to remove these modules as namespaces from the generated URLs we use scope module:. This works great but the problem ist that the generated links inside a response still include the module.

The code just checks the module hierarchy of the resource class and completely ignores how rails generates the routes. (See: https://github.com/cerebris/jsonapi-resources/blob/master/lib/jsonapi/link_builder.rb#L102).
Also see #361 for a similar problem.

As a workaround we monkey patch the formatted_module_path_from_class method to return the correct path \api\. But that is not an ideal solution.

An example route file:

namespace :api do
  scope module: :v1, constraint: ApiConstraint.new(version: 1) do
    jsonapi_resources :my_resource
  end
end

And an example resource:

module Api
  module V1
    class MyResource < JSONAPI::Resource
      [...]
    end
  end
end

So Rails generates routes like: /api/my-resource
But the generated links look like this: /api/v1/my-resource

Sascha

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions