her
her copied to clipboard
her and (Rails) shallow routes
I'm having an JSON API with endpoints like: POST /contacts/1/tasks to create a Task for a Contact.
Her is setup like:
class ApiModel
include Her::Model
include_root_in_json true
parse_root_in_json false
end
class Contact < ApiModel
has_many :tasks
end
class Task < ApiModel
belongs_to :contact
end
However when I try to do:
Contact.find(1).tasks.create(:some_attributes => 1)
It POSTs to /tasks instead of /contacts/1/tasks (in contrast with what the documentation says).
Any idea what I'm doing wrong here? or any way to make this work with shallow routes (see: http://edgeguides.rubyonrails.org/routing.html#shallow-nesting)? This would mean:
- POST (create) would go to /contacts/1/tasks and
- PUT (update) would need to go to /tasks/:id
I suspect you need need a collection_path in Task like " /contacts/:contact_id/tasks"
but the larger issue of shallow routes is my new thorn
"Her" seems to have no knowledge of this concept
update: this seems to work https://github.com/remiprev/her/blob/4824f6f18e4de091fede948cf362728c851f3654/lib/her/model/paths.rb#L80