FIX: Ensure load more directory items has a .json url

The `/directory_items` route needs to have a .json url, but the rails
url helper `_path` doesn't return the format of the route.

I tried passing in a format options to `directory_items_path`. Which
works in the rails console

```
[8] pry(main)> directory_items_path(params.merge(:format => :json))
=> "/directory_items.json?page=1"
```

but when I added that some logic to the controller it comes out as

```
/directory_items?format=json&page=1
```

(which is actually how I expect it to work based on how you pass in the
format param). Anyways, because I couldn't figure out how to pass a
format to the `_path` helper I just used URI.parse to append `.json`
manually.
This commit is contained in:
Blake Erickson 2020-08-11 12:35:23 -06:00
parent c38212c73e
commit ee366f7ac7
2 changed files with 4 additions and 1 deletions

View File

@ -65,6 +65,8 @@ class DirectoryItemsController < ApplicationController
more_params = params.slice(:period, :order, :asc).permit!
more_params[:page] = page + 1
load_more_uri = URI.parse(directory_items_path(more_params))
load_more_directory_items_json = "#{load_more_uri.path}.json?#{load_more_uri.query}"
# Put yourself at the top of the first page
if result.present? && current_user.present? && page == 0
@ -84,7 +86,7 @@ class DirectoryItemsController < ApplicationController
meta: {
last_updated_at: last_updated_at,
total_rows_directory_items: result_count,
load_more_directory_items: directory_items_path(more_params)
load_more_directory_items: load_more_directory_items_json
}
)
end

View File

@ -50,6 +50,7 @@ describe DirectoryItemsController do
expect(json['directory_items'].length).to eq(4)
expect(json['meta']['total_rows_directory_items']).to eq(4)
expect(json['meta']['load_more_directory_items']).to include('.json')
end
it "fails when the directory is disabled" do