Christian Fazzini
Christian Fazzini

Reputation: 19723

How to construct the following JSONAPI url in Rails?

Im following http://jsonapi.org/format/#document-top-level

I notice the following end-point (route):

/articles/1/relationships/author

In Rails, how would this route be contstructed in routes.rb?

resources :articles do
  # What goes here?
  # Should relationship be a namespace or other?
  # I guess author can be defined as a collection, or just a simple get
end

relationships doesn't need to have any of the 7 RESTFUL actions.

Upvotes: 0

Views: 410

Answers (3)

stwienert
stwienert

Reputation: 3632

The scope :'/relationships' didn't work for me (At least in Rails 5).

I got it working with this:

resources :articles do
  resources :author, path: 'relationships/author'
end

This will just use the AuthorController and map it to /articles/:id/relationships/author.

Upvotes: 0

Rubyrider
Rubyrider

Reputation: 3587

The route should be like following code snippets:

resources :articles do
   resources :relationships, :only => [] do
     collection do 
       get :author
     end
   end
end

This is how the route file should look like. Please let me know if any update needed.

Upvotes: 2

Cyril Duchon-Doris
Cyril Duchon-Doris

Reputation: 14029

Bouncing on my idea of concerns, as you probably will have to use this in several resources (the rails doc is quite well-written)

concern :has_author do
  scope '/relationships' do
    resource :author
  end
end

resources :articles, concerns :has_author

Which is equivalent to

resources :articles do
  scope :'/relationships' do
    resource :author
  end
end

Upvotes: 2

Related Questions